所以我有一个Stream<Collection<Long>>
,通过对另一个流进行一系列转换获得。
我需要做的是将Stream<Collection<Long>>
收集到一个Collection<Long>
中。
我可以像这样将它们全部收集到一个列表中:
<Stream<Collection<Long>> streamOfCollections = /* get the stream */;
List<Collection<Long>> listOfCollections = streamOfCollections.collect(Collectors.toList());
然后我可以通过遍历那个集合列表将它们合并成一个。
但是,我想象中一定有一种简单的方法可以使用 .map()
或 .collect()
将这些集合流合并成一个 Collection<Long>
。只是我无法想到如何实现。有什么想法吗?
flatMap
。 - Sotirios Delimanolis