欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java合并list方法代码实例

时间:2021-03-25 09:20:37|栏目:JAVA代码|点击:

这篇文章主要介绍了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);

上一篇:JAVA基于静态数组实现栈的基本原理与用法详解

栏    目:JAVA代码

下一篇:详解SpringBoot启动类的扫描注解的用法及冲突原则

本文标题:java合并list方法代码实例

本文地址:http://www.codeinn.net/misctech/87925.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有