假设我有一个包含集合的对象,上述集合中的每个元素都包含一个集合,并且每个集合都包含一个集合。嵌套集合lambda迭代
我想迭代最深的对象并将相同的代码应用到它。
命令式的方式是微不足道的,但有没有办法让lambda-fy这一切?
这是怎样的代码看起来今天:
My object o;
SecretType computedThingy = 78;
for (FirstLevelOfCollection coll : o.getList()) {
for (SecondLevelOfCollection colColl : coll.getSet()) {
for (MyCoolTinyObjects mcto : colColl.getFoo()) {
mcto.setSecretValue(computedThingy);
}
}
}
我可以看到如何做一个lambda了最深的循环:
colColl.getFoo().stream().forEach(x -> x.setSecretValue(computedThingy)
但我可以做多吗?
有'Collectors.flatMapping'在JDK-9还... – Eugene