Java 8实战--收集器(Collectors中的静态方法)
1、转化为set/list/具体类型HashSet(toSet(),toList(),toCollection(HashSet::new))
2、求和、平均、最大、最小、个数(summarizingInt,averagingInt等 )
3、连接(joining),依次使用函数求值(reducing)
4、分组(一级、二级分组、联合使用其他函数)(groupingBy)
5、分区--分组的特例,按照key是boolean类型分组(也可基于一级分区进行二级操作)(partitioningBy)
方法说明及汇总:(摘自《Java 8 实战》6.5节)
实践代码如下:
package com.csdn.test;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
public class TestStreamCollect
{
/**
* 也可实现自定义收集器 需要实现Collector接口
*/
public static void testCollect(List intList)
{
// 求和/平均/最大/最小/数量
IntSummaryStatistics sum = intList.stream().collect(Collectors.summarizingInt(i->i));
System.out.println("sum-->" + sum.getSum());
System.out.println("avg-->" + sum.getAverage());
double d = intList.stream().collect(Collectors.averagingInt(i->i));
System.out.println("avg-->" + d);
/*====================================================================*/
// 连接
String str = intList.stream().map(i -> i+"").collect(Collectors.joining(", "));
System.out.println("string concat-->" + str);
/*====================================================================*/
// reducing求和
// collect方法特别适合表达可变容器上的归约,不要用reduce代替
int sumReduce = intList.stream().collect(Collectors.reducing(0, val->val, (i,j)->i+j));
System.out.println("sum-->" + sumReduce);
/*====================================================================*/
// 一级分组
Map> gMap = intList.stream().collect(Collectors.groupingBy(i->i));
System.out.println("gMap->" + gMap);
// 二级分组
Map>> gSecMap = intList.stream().collect(Collectors.groupingBy(i->i,Collectors.groupingBy(i->i)));
System.out.println("gMap->" + gSecMap);
// 按组收集数据
Map gSubMap = intList.stream().collect(Collectors.groupingBy(i->i,Collectors.counting()));
System.out.println("gMap->" + gSubMap);
// 按照组统计最大值,可读性很差,不推荐
Map compareMap = intList.stream()
.collect(Collectors.groupingBy(i -> i,
Collectors.collectingAndThen(
Collectors.maxBy(
Comparator.comparingInt(i -> i)),
Optional::get)));
System.out.println("compareMap->" + compareMap);
/*====================================================================*/
// 与groupingBy联合使用
Map gSumMap = intList.stream().collect(
Collectors.groupingBy(i -> i, Collectors.summarizingInt(i -> i)));
System.out.println("gSumMap->" + gSumMap);
Map> gSetMap = intList.stream().collect(
Collectors.groupingBy(i -> i, Collectors.mapping(i->i, Collectors.toSet())));
System.out.println("gSetMap->" + gSetMap);
// 指定set具体类型为HashSet
Map> gHashSetMap = intList.stream().collect(
Collectors.groupingBy(i -> i, Collectors.mapping(i->i, Collectors.toCollection(HashSet::new))));
System.out.println("gHashSetMap->" + gHashSetMap);
/*====================================================================*/
// 分区--分组的特例,按照key是boolean类型分组(也可基于一级分区进行二级操作)
Map> gBoolMap = intList.stream().collect(Collectors.partitioningBy(i->i==2));
System.out.println("gBoolMap->" + gBoolMap);
}
public static void main(String[] args)
{
Integer[] numbers = {2, 2, 3, 5, 7, 11, 13};
List numList = Arrays.asList(numbers);
// 使用toArray list-->array
System.out.println(Arrays.asList(numList.toArray(new Integer[0])));
testCollect(numList);
}
}
输出:
sum-->43
avg-->6.142857142857143
avg-->6.142857142857143
string concat-->2, 2, 3, 5, 7, 11, 13
sum-->43
gMap->{2=[2, 2], 3=[3], 5=[5], 7=[7], 11=[11], 13=[13]}
gMap->{2={2=[2, 2]}, 3={3=[3]}, 5={5=[5]}, 7={7=[7]}, 11={11=[11]}, 13={13=[13]}}
gMap->{2=2, 3=1, 5=1, 7=1, 11=1, 13=1}
compareMap->{2=2, 3=3, 5=5, 7=7, 11=11, 13=13}
gSumMap->{2=IntSummaryStatistics{count=2, sum=4, min=2, average=2.000000, max=2}, 3=IntSummaryStatistics{count=1, sum=3, min=3, average=3.000000, max=3}, 5=IntSummaryStatistics{count=1, sum=5, min=5, average=5.000000, max=5}, 7=IntSummaryStatistics{count=1, sum=7, min=7, average=7.000000, max=7}, 11=IntSummaryStatistics{count=1, sum=11, min=11, average=11.000000, max=11}, 13=IntSummaryStatistics{count=1, sum=13, min=13, average=13.000000, max=13}}
gSetMap->{2=[2], 3=[3], 5=[5], 7=[7], 11=[11], 13=[13]}
gHashSetMap->{2=[2], 3=[3], 5=[5], 7=[7], 11=[11], 13=[13]}
gBoolMap->{false=[3, 5, 7, 11, 13], true=[2, 2]}
参考书籍:
《Java8实战》