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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。