簡體   English   中英

在Java中將多個集合組合成一個流的最簡單方法是什么?

[英]What's the easiest way to combine multiple collections into a stream in Java?

假設我有多個集合,我想將其作為單個流處理。 最簡單的方法是什么? 是否有可以為我做這個的實用工具類,或者我必須自己滾動一些東西?

如果我的問題不明確,這基本上就是我想要做的:

Collection<Region> usaRegions;
Collection<Region> canadaRegions;
Collection<Region> mexicoRegions;
Stream<Region> northAmericanRegions = collect(usaRegions, canadaRegions, mexicoRegions);

public Stream<T> collect(T...) {
     /* What goes here? */
}

或者,您可以使用flatMap:

Stream<Region> = 
    Stream.of(usaRegions, canadaRegions, mexicoRegions)
          .flatMap(Collection::stream);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM