1. 程式人生 > 程式設計 >java合併list方法程式碼例項

java合併list方法程式碼例項

這篇文章主要介紹了java合併list方法程式碼例項,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

List包括List介面以及List介面的所有實現類。因為List介面實現了Collection介面,所以List介面擁有Collection介面提供的所有常用方法,又因為List是列表型別,所以List介面還提供了一些適合於自身的常用方法

方法一

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

方法二

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);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。