Java 8:将列表与流 API 合并
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23112874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java 8: merge lists with stream API
提问by mat_boy
I have the following situation
我有以下情况
Map<Key, ListContainer> map;
public class ListContainer{
List<AClass> lst;
}
I have to merge all the lists lst
from the ListContainer
objects from a Map
map.
我已经合并所有列表lst
从ListContainer
从对象Map
地图。
public static void main(String args[]){
List<AClass> alltheObjectsAClass = map.values().stream(). // continue....
}
Any idea how, using Java 8 stream API?
知道如何使用 Java 8 流 API 吗?
采纳答案by Puce
回答by Skywalker
Alternative: Stream.concat()
替代方案:Stream.concat()
Stream.concat(map.values().stream(), listContainer.lst.stream())
.collect(Collectors.toList()
回答by Konzern
In Java 8 we can use stream List1.stream().collect(Collectors.toList()).addAll(List2); Another option List1.addAll(List2)
在 Java 8 中,我们可以使用流 List1.stream().collect(Collectors.toList()).addAll(List2); 另一个选项 List1.addAll(List2)