赞
踩
// flatMap:接收一个T返回一个R,将一个元素转为一个新的流 ;R apply(T t);
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
// flatMap: 将一个元素转为一个新的流 1-N
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);
// reduce:接收一个BinaryOperator类型的函数式接口,将多个元素归并为一个
Optional<T> reduce(BinaryOperator<T> accumulator);
// reduce: 将多个元素归并为一个 N-1
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());
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")); // Stream<Qiu> concat = Stream.concat(list1.stream(), list2.stream()); 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);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。