Stream流
构建:
StreamBuilder流构建器,动态
Stream<String> stream = list1.stream();
合并 concat
Stream<Integer> stream1 = Stream.of(1, 2, 3);
Stream<Integer> stream2 = Stream.of(4, 5, 6);
Stream<Integer> concatenatedStream = Stream.concat(stream1, stream2);
聚合 reduce
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
int sum = stream.reduce(0, Integer::sum);
过滤器 filter
List<People> collect = list.stream() .filter(people11 -> people11.getAge() > 22) .collect(Collectors.toList());
Map
取值
List collect = list1.stream().map(people -> people.getAge()).collect(Collectors.toList());
类型转化
List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); List collect = list.stream().map(String::valueOf).collect(Collectors.toList());
flatMap 处理嵌套
List<List<People>> list3 = new ArrayList<>();
list3.stream().flatMap(listPeople -> listPeople.stream()).collect(Collectors.toList()).forEach(System.out::println);
List<List<List<People>>> list4 = new ArrayList<>(); list4.stream().flatMap(lists -> lists.stream().flatMap(listPeople -> listPeople.stream())).collect(Collectors.toList()).forEach(System.out::println);
分组
Map<String, List<People>> collect1 = list.stream() .collect(Collectors.groupingBy(People::getCountry));
并行流
list.stream().parallel() list.parallelStream()
