[英]Not being able to stop at exception when using consecutive stream.flatMap
[英]How do I combine the object being flattened with the flattened objects using Stream.flatmap
我有以下课程的流:
class A {
Stream<B> bs;
}
我想将其转换为以下类的流:
class AB {
A a;
B b;
AB(A a, B b) {
this.a = a;
this.b = b;
}
}
我可以看到flatMap
会给我一个 B 流:
astream.flatMap(a -> a.bs);
但我不知道如何将对象传递到地图,例如:
astream.flatMap(a -> a.bs).map(b -> new AB(a, b));
如果我尝试这个,我会收到一个错误:
error: cannot find symbol
我有点期待。
你必须映射B
S中的bs
Stream
至AB
S使用map
:
List<AB> abs = astream.flatMap(a -> a.bs.map(b->new AB(a,b)))
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.