Java merge list method code example

Time:2020-11-30

This article mainly introduces the Java merge list method code example, the article through the example code introduction is very detailed, to everybody’s study or the work has certain reference study value, needs the friend may refer to

List includes the list interface and all implementation classes of the list interface. Because the list interface implements the collection interface, the list interface has all the common methods provided by the collection interface. Because list is a list type, the list interface also provides some common methods suitable for itself

Method 1


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

Method 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 ("pockmarks", 90, 989.2);
mazi.setChildren(childrens1);
Employee Lisi = new employee ("Li Si", 10, 1000.1);
lisi.setChildren(childrens2);
List<Employee> list = Arrays.asList(mazi,lisi);

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.

Recommended Today

Api: tiktok: user video list

Tiktok tiktok, tiktok, tiktok, Api, Api, jitter, voice, bullet screen comments, jitter, jitter, and jitter. Tiktok tiktok tiktok data, jitter data acquisition, live broadcast of shaking sound Titodata: professional short video data acquisition and processing platform. For more information, please contact:TiToData Massive data collection Collect 500 million pieces of data for customers every day Tiktok […]