Stream类的collect方法
参考: https://www.jianshu.com/p/ccbb42ad9551
- Collectors.toSet():转换成set集合。
- Collectors.toCollection(TreeSet::new):转换成特定的set集合。
TreeSet<Integer> collect2 = Stream.of(1, 3, 4).collect(Collectors.toCollection(TreeSet::new)); - Collectors.toMap(x -> x, x -> x + 1):转换成map。
Map<Integer, Integer> collect1 = Stream.of(1, 3, 4).collect(Collectors.toMap(x -> x, x -> x + 1)); - Collectors.minBy(Integer::compare):求最小值,相对应的当然也有maxBy方法。
- Collectors.averagingInt(x->x):求平均值,同时也有averagingDouble、averagingLong方法。
System.out.println(Stream.of(1, 2, 3).collect(Collectors.averagingInt(x->x))); - Collectors.summingInt(x -> x)):求和。
- Collectors.summarizingDouble(x -> x):可以获取最大值、最小值、平均值、总和值、总数。
DoubleSummaryStatistics summaryStatistics = Stream.of(1, 3, 4).collect(Collectors.summarizingDouble(x -> x));
summaryStatistics.getAverage();//平均值 - Collectors.groupingBy(x -> x)有三种方法,查看源码可以知道前两个方法最终调用第三个方法,第二个参数默认HashMap::new 第三个参数默认Collectors.toList(),参考SQL的groupBy。
Map<Integer, List<Integer>> map = Stream.of(1, 3, 3, 4).collect(Collectors.groupingBy(x -> x));
Map<Integer, Long> map = Stream.of(1, 3, 3, 4).collect(Collectors.groupingBy(x -> x, Collectors.counting()));
HashMap<Integer, Long> hashMap = Stream.of(1, 3, 3, 4).collect(Collectors.groupingBy(x -> x, HashMap::new, Collectors.counting())); - Collectors.partitioningBy(x -> x > 2),把数据分成两部分,key为ture/false。第一个方法也是调用第二个方法,第二个参数默认为Collectors.toList()。
Map<Boolean, List<Integer>> collect5 = Stream.of(1, 3, 4).collect(Collectors.partitioningBy(x -> x > 2));
Map<Boolean, Long> collect4 = Stream.of(1, 3, 4).collect(Collectors.partitioningBy(x -> x > 2, Collectors.counting())); - Collectors.joining(","):拼接字符串。
System.out.println(Stream.of("a", "b", "c").collect(Collectors.joining(","))); - Collectors.reducing(0, x -> x + 1, (x, y) -> x + y)):在求累计值的时候,还可以对参数值进行改变,这里是都+1后再求和。跟reduce方法有点类似,但reduce方法没有第二个参数。
System.out.println(Stream.of(1, 3, 4).collect(Collectors.reducing(0, x -> x + 1, (x, y) -> x + y))); - Collectors.collectingAndThen(Collectors.joining(","), x -> x + "d"):先执行collect操作后再执行第二个参数的表达式。这里是先拼接字符串,再在最后+ "d"。
String str= Stream.of("a", "b", "c").collect(Collectors.collectingAndThen(Collectors.joining(","), x -> x + "d")); - Collectors.mapping(...):跟map操作类似,只是参数有点区别。
System.out.println(Stream.of("a", "b", "c").collect(Collectors.mapping(x -> x.toUpperCase(), Collectors.joining(","))));
链接:https://www.jianshu.com/p/ccbb42ad9551
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
===================
public static void test003() throws Exception { TreeSet<Integer> collect2 = Stream.of(1, 3, 4, 2).collect(Collectors.toCollection(TreeSet::new)); System.out.println(collect2);// [1, 2, 3, 4] List<KeyValue<Integer, String>> kvs = Lists.newArrayList(); for (int i = 0; i < 3; i++) { kvs.add(new KeyValue<>(i, "hello-" + i)); } Map<Integer, String> map = kvs.stream().collect(Collectors.toMap(KeyValue::getKey, kv -> { return kv.getValue() + "&&" + kv.getKey(); })); System.out.println(map);// {0=hello-0&&0, 1=hello-1&&1, 2=hello-2&&2} String joinResult = Stream.of("a", "b", "c") .collect(Collectors.mapping(x -> x.toUpperCase(), Collectors.joining(","))); System.out.println(joinResult);// A,B,C Map<Integer, Integer> collect1 = Stream.of(1, 3, 4).collect(Collectors.toMap(x -> x, x -> x + 1)); System.out.println(collect1);// {1=2, 3=4, 4=5} // Collectors.joining(","):拼接字符串。 System.out.println(Stream.of("a", "b", "c").collect(Collectors.joining(",")));// a,b,c System.out.println(Stream.of("a", "b", "c") .collect(Collectors.collectingAndThen(Collectors.joining(","), x -> x + "d")));// a,b,cd }