1.FlagMap
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
示例代码
List<String> asList = Arrays.asList("zs", "ls", "ww", "zl", "tq");
List<String> stringList = asList.stream()
.flatMap(item -> Arrays.asList(("1" + item).toUpperCase(), ("2" + item).toUpperCase(), ("3" + item).toUpperCase()).stream())
.collect(Collectors.toList());
System.out.println("stringList = " + stringList);
2.Reduce
Optional<T> reduce(BinaryOperator<T> accumulator);
示例代码
List<Integer> asList = Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> optional = asList.stream().reduce((x, y) -> x + y);
System.out.println("optional.get() = " + optional.get());
3.Concat合并流
List<Qiu> list1 = Arrays.asList(new Qiu(1L, "AA"), new Qiu(2L, "BB"), new Qiu(3L, "CC"));
List<Qiu> list2 = Arrays.asList(new Qiu(3L, "DD"), new Qiu(2L, "EE"), new Qiu(5L, "FF"));
Map<Long, Qiu> map = new HashMap<>();
Stream.concat(list1.stream(), list2.stream()).forEach(item -> {
Qiu qiu = item;
if (map.containsKey(item.getId())) {
qiu = map.get(item.getId());
qiu.setName(qiu.getName() + "---" + item.getName());
}
map.put(item.getId(), qiu);
});
System.out.println("map = " + map);
List<Qiu> qiuList = map.values().stream().collect(Collectors.toList());
System.out.println("qiuList = " + qiuList);