Java Collector和Collectors
作为java8的一个特性Collectors
一般用于流处理中的最后一步(也就是 Stream.collect()),是JDK预实现Collector
的工具类,它内部提供了多种Collector
,我们可以直接拿来使用,非常方便。而Collector
是专门用来作为流(Stream)的collect方法的参数,说白了Collectors
是作为生产具体Collector
的工具类,
流: Java 8 API添加了一个新的抽象称为流(Stream),可以让你以一种声明的方式处理数据。
Stream
的使用一种类似用SQL
语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象,可以让程序员写出高效率、干净、简洁的代码。
流处理: 流(Stream)在管道中传输,并且可以在管道的各个节点上进行处理,比如筛选,排序,聚合等,这叫流处理。
流处理最后一步: 元素流在管道中经过一系列的中间处理操作(intermediate operation),最后由最终操作(terminal operation)得到前>面处理的结果,这一步就是流处理最后一步了。
Collector
Collector主要包含五个参数,它的行为也是由这五个参数来定义的,如下所示:
public interface Collector<T, A, R> {
// supplier参数用于生成结果容器,容器类型为A
Supplier<A> supplier();
// accumulator用于消费元素,也就是归纳元素,这里的T就是元素,它会将流中的元素一个一个与结果容器A发生操作
BiConsumer<A, T> accumulator();
// combiner用于两个两个合并并行执行的线程的执行结果,将其合并为一个最终结果A
BinaryOperator<A> combiner();
// finisher用于将之前整合完的结果R转换成为A
Function<A, R> finisher();
// characteristics表示当前Collector的特征值,这是个不可变Set
Set<Characteristics> characteristics();
}
Collector拥有两个of方法用于生成Collector实例,其中一个拥有上面所有五个参数,另一个四个参数,不包括finisher。
public interface Collector<T, A, R> {
// 四参方法,用于生成一个Collector,T代表流中的一个一个元素,R代表最终的结果
public static<T, R> Collector<T, R, R> of(Supplier<R> supplier,
BiConsumer<R, T> accumulator,
BinaryOperator<R> combiner,
Characteristics... characteristics) {/*...*/}
// 五参方法,用于生成一个Collector,T代表流中的一个一个元素,A代表中间结果,R代表最终结果,finisher用于将A转换为R
public static<T, A, R> Collector<T, A, R> of(Supplier<A> supplier,
BiConsumer<A, T> accumulator,
BinaryOperator<A> combiner,
Function<A, R> finisher,
Characteristics... characteristics) {/*...*/}
}
Characteristics:这个特征值是一个枚举,拥有三个值:CONCURRENT(多线程并行),UNORDERED(无序),IDENTITY_FINISH(无需转换结果)。其中四参of方法中没有finisher参数,所以必有IDENTITY_FINISH特征值。
Collectors
Collectors
是一个工具类,是JDK预实现Collector的工具类,它内部提供了多种Collector
,我们可以直接拿来使用,非常方便。
toCollection
将流中的元素全部放置到一个集合中返回,这里使用Collection,泛指多种集合。
public class CollectorsTest {
public static void toCollectionTest(List<String> list) {
List<String> ll = list.stream().collect(Collectors.toCollection(LinkedList::new));
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
toCollectionTest(list);
}
}
toList
将流中的元素放置到一个列表集合中去。这个列表默认为ArrayList。
public class CollectorsTest {
public static void toListTest(List<String> list) {
List<String> ll = list.stream().collect(Collectors.toList());
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
toListTest(list);
}
}
toSet
将流中的元素放置到一个无序集set中去。默认为HashSet。
public class CollectorsTest {
public static void toSetTest(List<String> list) {
Set<String> ss = list.stream().collect(Collectors.toSet());
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
toSetTest(list);
}
}
joining
joining的目的是将流中的元素全部以字符序列的方式连接到一起,可以指定连接符,甚至是结果的前后缀。
public class CollectorsTest {
public static void joiningTest(List<String> list){
// 无参方法
String s = list.stream().collect(Collectors.joining());
System.out.println(s);
// 指定连接符
String ss = list.stream().collect(Collectors.joining("-"));
System.out.println(ss);
// 指定连接符和前后缀
String sss = list.stream().collect(Collectors.joining("-","S","E"));
System.out.println(sss);
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
joiningTest(list);
}
}
mapping
public class CollectorsTest {
public static void mapingTest(List<String> list){
List<Integer> ll = list.stream().limit(5).collect(Collectors.mapping(Integer::valueOf,Collectors.toList()));
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
mapingTest(list);
}
}
collectingAndThen
该方法是在归纳动作结束之后,对归纳的结果进行再处理。
public class CollectorsTest {
public static void collectingAndThenTest(List<String> list){
int length = list.stream().collect(Collectors.collectingAndThen(Collectors.toList(),e -> e.size()));
System.out.println(length); //8
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
collectingAndThenTest(list);
}
}
counting
该方法用于计数。
public class CollectorsTest {
public static void countingTest(List<String> list){
long size = list.stream().collect(Collectors.counting());
System.out.println(size); //8
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
countingTest(list);
}
}
minBy/maxBy
生成一个用于获取最小/最大值的Optional结果的Collector。
public class CollectorsTest {
public static void maxByAndMinByTest(List<String> list){
System.out.println(list.stream().collect(Collectors.maxBy((a,b) -> a.length()-b.length())));
System.out.println(list.stream().collect(Collectors.minBy((a,b) -> a.length()-b.length())));
/**
*输出
*Optional[212121121]
*Optional[123]
**/
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
maxByAndMinByTest(list);
}
}
summingInt/summingLong/summingDouble
生成一个用于求元素和的Collector,首先通过给定的mapper将元素转换类型,然后再求和。参数的作用就是将元素转换为指定的类型,最后结果与转换后类型一致。
public class CollectorsTest {
public static void summingTest(List<String> list){
int i = list.stream().limit(3).collect(Collectors.summingInt(Integer::valueOf));
long l = list.stream().limit(3).collect(Collectors.summingLong(Long::valueOf));
double d = list.stream().limit(3).collect(Collectors.summingDouble(Double::valueOf));
System.out.println(i +"\n" +l + "\n" + d);
/**输出
*1368
*1368
*1368.0
**/
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
summingTest(list);
}
}
averagingInt/averagingLong/averagingDouble
生成一个用于求元素平均值的Collector,首选通过参数将元素转换为指定的类型。参数的作用就是将元素转换为指定的类型,求平均值涉及到除法操作,结果一律为Double类型。
public class CollectorsTest {
public static void averagingTest(List<String> list){
double i = list.stream().limit(3).collect(Collectors.averagingInt(Integer::valueOf));
double l = list.stream().limit(3).collect(Collectors.averagingLong(Long::valueOf));
double d = list.stream().limit(3).collect(Collectors.averagingDouble(Double::valueOf));
System.out.println(i +"\n" +l + "\n" + d);
/**输出
*456.0
*456.0
*456.0
**/
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
averagingTest(list);
}
}
reducing
reducing方法有三个重载方法,其实是和Stream里的三个reduce方法对应的,二者是可以替换使用的,作用完全一致,也是对流中的元素做统计归纳作用。
public class CollectorsTest {
public static void reducingTest(List<String> list){
System.out.println(list.stream().limit(4).map(String::length).collect(Collectors.reducing(Integer::sum)));
System.out.println(list.stream().limit(3).map(String::length).collect(Collectors.reducing(0, Integer::sum)));
System.out.println(list.stream().limit(4).collect(Collectors.reducing(0,String::length,Integer::sum)));
/**输出
*Optional[13]
*9
*13
**/
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
reducingTest(list);
}
}
groupingBy
这个方法是用于生成一个拥有分组功能的Collector,它也有三个重载方法:
public class CollectorsTest {
public static void groupingByTest(List<String> list){
Map<Integer,List<String>> s = list.stream().collect(Collectors.groupingBy(String::length));
Map<Integer,List<String>> ss = list.stream().collect(Collectors.groupingBy(String::length, Collectors.toList()));
Map<Integer,Set<String>> sss = list.stream().collect(Collectors.groupingBy(String::length,HashMap::new,Collectors.toSet()));
System.out.println(s.toString() + "\n" + ss.toString() + "\n" + sss.toString());
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
groupingByTest(list);
}
}
groupingBy方法还有并发版的groupingByConcurrent,功能基本一致,只是返回的Collector是并行的。
partitioningBy
该方法将流中的元素按照给定的校验规则的结果分为两个部分,放到一个map中返回,map的键是Boolean类型,值为元素的列表List。
public class CollectorsTest {
public static void partitioningByTest(List<String> list){
Map<Boolean,List<String>> map = list.stream().collect(Collectors.partitioningBy(e -> e.length()>5));
Map<Boolean,Set<String>> map2 = list.stream().collect(Collectors.partitioningBy(e -> e.length()>6,Collectors.toSet()));
System.out.println(map.toString() + "\n" + map2.toString());
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
partitioningByTest(list);
}
}
toMap
toMap方法是根据给定的键生成器和值生成器生成的键和值保存到一个map中返回,键和值的生成都依赖于元素,可以指定出现重复键时的处理方案和保存结果的map。
public class CollectorsTest {
public static void toMapTest(List<String> list){
Map<String,String> map = list.stream().limit(3).collect(Collectors.toMap(e -> e.substring(0,1),e -> e));
Map<String,String> map1 = list.stream().collect(Collectors.toMap(e -> e.substring(0,1),e->e,(a,b)-> b));
Map<String,String> map2 = list.stream().collect(Collectors.toMap(e -> e.substring(0,1),e->e,(a,b)-> b,HashMap::new));
System.out.println(map.toString() + "\n" + map1.toString() + "\n" + map2.toString());
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
toMapTest(list);
}
}
summarizingInt/summarizingLong/summarizingDouble
这三个方法适用于汇总的,返回值分别是IntSummaryStatistics,LongSummaryStatistics,DoubleSummaryStatistics。在这些返回值中包含有流中元素的指定结果的数量、和、最大值、最小值、平均值。所有仅仅针对数值结果。
public class CollectorsTest {
public static void summarizingTest(List<String> list){
IntSummaryStatistics intSummary = list.stream().collect(Collectors.summarizingInt(String::length));
LongSummaryStatistics longSummary = list.stream().limit(4).collect(Collectors.summarizingLong(Long::valueOf));
DoubleSummaryStatistics doubleSummary = list.stream().limit(3).collect(Collectors.summarizingDouble(Double::valueOf));
System.out.println(intSummary.toString() + "\n" + longSummary.toString() + "\n" + doubleSummary.toString());
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
summarizingTest(list);
}
}
最后我们可以从返回的汇总实例中获取到想要的汇总结果。
总结
整个Collectors工具类就是在为Collector服务,用于创建各种不同的Collector。部分功能与Stream中的方法重合了,为了简化代码,完全不必采用Collectors实现,优先Stream方法。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库