List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法
方法一
Listreduce = list.stream() .map(x -> x.getChildren()) .reduce(new ArrayList<>(), (all, item) -> { all.addAll(item); return all; }); System.out.println(reduce);
方法二
Listcollect = 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);