這篇文章主要介紹了java合并list方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
List包括List接口以及List接口的所有實現類。因為List接口實現了Collection接口,所以List接口擁有Collection接口提供的所有常用方法,又因為List是列表類型,所以List接口還提供了一些適合于自身的常用方法
方法一
List reduce = list.stream()
.map(x -> x.getChildren())
.reduce(new ArrayList<>(), (all, item) -> {
all.addAll(item);
return all;
});
System.out.println(reduce);
方法二
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);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。