首页 > 编程语言 > 详细

java 合并list的方法

时间:2020-01-03 00:27:46      阅读:141      评论:0      收藏:0      [点我收藏+]
1.
List<Children> reduce = list.stream()
.map(x -> x.getChildren())
.reduce(new ArrayList<>(), (all, item) -> {
all.addAll(item);
return all;
});
System.out.println(reduce);

2.
List<Children> collect = list.stream()
.map(Employee::getChildren)
.flatMap(Collection::stream)
.distinct().collect(Collectors.toList());
----------------------------
List<Children> childrens1 = new ArrayList<>();
for (long i =0;i<10;i++){
childrens1.add(new Children(i,"x".concat(i+"")));
}
List<Children> 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<Employee> list = Arrays.asList(mazi,lisi);

java 合并list的方法

原文:https://www.cnblogs.com/wanjn/p/12142701.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!