java 合并list的方法

1.
List reduce = list.stream()
.map(x -> x.getChildren())
.reduce(new ArrayList<>(), (all, item) -> {
all.addAll(item);
return all;
});
System.out.println(reduce);

2.
List collect = list.stream()
.map(Employee::getChildren)
.flatMap(Collection::stream)
.distinct().collect(Collectors.toList());
----------------------------
List childrens1 = new ArrayList<>();
for (long i =0;i<10;i++){
childrens1.add(new Children(i,"x".concat(i+"")));
}
List childrens2 = new ArrayList<>();
for (long i =100;i<110;i++){
childrens2.add(new Children(i,"x".concat(i+"")));
}
Employee mazi = new Employee("麻子", 90, 989.2);
mazi.setChildren(childrens1);
Employee lisi = new Employee("李四", 10, 1000.1);
lisi.setChildren(childrens2);
List list = Arrays.asList(mazi,lisi);

你可能感兴趣的:(java 合并list的方法)