java8 自定义收集器Collector

2024-05-31 18:58

本文主要是介绍java8 自定义收集器Collector,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Collectors类的一些静态方法
这里写图片描述

import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collector.Characteristics;
import java.util.stream.Collectors;import org.junit.Test;import com.cbzk.lambda.Employee;
import com.cbzk.lambda.Employee.Status;/** 自定义收集器* 需要重写collector接口* public interface Collector<T, A, R> {Supplier<A> supplier();BiConsumer<A, T> accumulator();Function<A, R> finisher();  //最后要做的转换操作BinaryOperator<A> combiner();Set<Characteristics> characteristics();
}
T要收集项目的泛型
A累加器的类型
R收集操作得到的对象(不一定是集合)Characteristics是一个包含三个项目的枚举。
UNORDERED——归约结果不受流中项目的遍历和累积顺序的影响。
CONCURRENT——accumulator函数可以从多个线程同时调用,且该收集器可以并行归
约流。如果收集器没有标为UNORDERED, 那它仅在用于无序数据源时才可以并行归约。
IDENTITY_FINISH——这表明完成器方法返回的函数是一个恒等函数,可以跳过。这种情况下,累加器对象将会直接用作归约过程的最终结果。这也意味着,将累加器A不加检
查地转换为结果R是安全的。
我们迄今开发的ToListCollector是IDENTITY_FINISH的,因为用来累积流中元素
List已经是我们要的最终结果,用不着进一步转换了,但它并不是UNORDERED,因为用在有序
流上的时候,我们还是希望顺序能够保留在得到的List中。最后,它是CONCURRENT的,但我们
刚才说过了,仅仅在背后的数据源无序时才会并行处理//通过collect代码认识一下:if (isParallel()&& (collector.characteristics().contains(Collector.Characteristics.CONCURRENT))&& (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) {//并发}
即并发条件:
在Collector.Characteristics.CONCURRENT的基础上,收集器Collector具有Characteristics.UNORDERED特性或者原始数据是无序的时才应用并发;String concat = stringStream.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();发现了一个小现象
在使用java8的lambda时,
有的编译报错并不是因为你写的有问题,而是你没有写返回值造成的;
比如: HashMap<String, Integer> map3 = emps.stream().collect(HashMap::new, (m, emp) -> m.put(emp.getName(), emp.getAge()),HashMap::putAll);
直接写 emps.stream().collect(HashMap::new, (m, emp) -> m.put(emp.getName(), emp.getAge()),HashMap::putAll);就会报错,编译器不认识m到底是什么,估计跟前面的 HashMap<String, Integer>推断有关;参考:                  
http://blog.jobbole.com/104067/*/
public class StreamDemo6 {List<Employee> emps = Arrays.asList(new Employee(102, "李四aa", 59, 6666.66, Status.BUSY),new Employee(102, "李四aaa", 60, 6666.66, Status.BUSY),new Employee(101, "张三bb", 18, 9999.99, Status.FREE),new Employee(103, "王五cc", 28, 3333.33, Status.VOCATION),new Employee(104, "赵六dd", 8, 7777.77, Status.BUSY),new Employee(104, "赵六dd", 9, 7777.77, Status.FREE),new Employee(104, null, 25, 7777.77, Status.FREE),new Employee(105, "丽丽", null, 5555.55, Status.BUSY));//实现toList的功能//顺序流@Testpublic void test() {List<Employee> res = emps.stream().limit(2).collect(Collectors.toList());//方式1 自定义收集器List<Employee> res2 = emps.stream().limit(2).collect(new MyToList<Employee>());System.out.println(res2);//方式2 重写collect方法的三个参数//这种直接写的不灵活,而且不容易看懂,且不能设置属性,其属性默认为IDENTITY_FINISH,CONCURRENT
//      collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner);//注意:第三个合并参数,Collector里面是BinaryOperator,而collect里面是BiConsumer//所以可以直接List<Employee> res3 = emps.stream().limit(2)
//          .collect(ArrayList::new, List::add,(list1,list2)->list1.addAll(list2));.collect(ArrayList::new,List::add,List::addAll);System.out.println(res3);//方式3:使用Collector.of 这个可以理解成自定义收集器的 完整封装版  List<Employee> res4=emps.stream().limit(2).collect(Collector.of(ArrayList::new, List::add, (list1,list2)->{list1.addAll(list2);return list1;}, new Characteristics[]   {Characteristics.IDENTITY_FINISH,Characteristics.CONCURRENT}));System.out.println(res4);}//并行流
//  list集合本身是有序的,所以并行不起来@Testpublic void test1() {List<Employee> res = emps.stream().limit(2).parallel().collect(Collectors.toList());System.out.println(res);List<Employee> res2 = emps.stream().parallel().collect(new MyToList<Employee>());System.out.println(res2);List<Employee> res3 = emps.stream().parallel().collect(ArrayList::new,List::add,List::addAll);System.out.println(res3);}//实现tomap的功能//顺序流@Testpublic void test2() {//一般转换map  //它存在2个问题,1.对于重复key报错  2.对于vlauenull报错
//      Map<String, Integer> map1 = emps.stream()
//          .collect(Collectors.toMap(Employee::getName, Employee::getAge));
//      System.out.println(map1);//解决方式1:使用collect的三个入参 自定义收集器Map<String,Integer> map2= emps.stream().collect(HashMap::new,( map, emp) ->                                                                                                                                                                           map.put(emp.getName(),emp.getAge()),Map::putAll);System.out.println(map2);//解决方式2:自已自定义收集器Map<String, Integer> map3 = emps.stream().collect(MyToMap.toMap(Employee::getName, Employee::getAge));System.out.println(map3);//因为toMap方法里面使用merge方法,不允许valuenull的,所以要使其避免那2个问题,必须绕开merge方法//解决方式3  使用 Collector.of方法  其实就是方式2的简写版//当使用顺序流使用时,(hmap1,hmap2)->{hmap1.putAll(hmap2);return hmap1; 没有作用,可以直接抛异常处理//当使用并行流时,(hmap1,hmap2)->{hmap1.putAll(hmap2);return hmap1;就需要这句,但并不一定是并行Map<String,Integer> map4=emps.stream().collect(Collector.of(HashMap::new, ( map, emp) -> map.put(emp.getName(),emp.getAge()), (hmap1,hmap2)->{ throw new UnsupportedOperationException();}, new Characteristics[]{Characteristics.IDENTITY_FINISH}));System.out.println(map4);//小结:上面的解决方式本质都是:自定义构造器}//并行流//合并函数需要这句:(hmap1,hmap2)->{hmap1.putAll(hmap2);return hmap1;@Testpublic void test2_1()  {//方式1Map<String,Integer> map1= emps.stream().parallel().collect(HashMap::new,( map, emp) -> map.put(emp.getName(),emp.getAge()),Map::putAll);System.out.println(map1);//方式2Map<String, Integer> map2 = emps.stream().parallel().collect(MyToMap.toMap(Employee::getName, Employee::getAge,(hmap1,hmap2)->{hmap1.putAll(hmap2);return hmap1;}));System.out.println(map2);//方式3Map<String,Integer> map3=emps.stream().parallel().collect(Collector.of(HashMap::new, ( map, emp) -> map.put(emp.getName(),emp.getAge()), (hmap1,hmap2)->{hmap1.putAll(hmap2);return hmap1;}, new Characteristics[]{Characteristics.IDENTITY_FINISH}));System.out.println(map3);           }}//如果MyToList不带泛型,那么后面的参数就是具体的类了
class MyToList<T> implements Collector<T, List<T>, List<T>>{//初始化@Overridepublic Supplier<List<T>> supplier() {
//      return ()->new ArrayList<>();return ArrayList::new;}//累计遍历,累加器@Overridepublic BiConsumer<List<T>, T> accumulator() {return List::add;}//合并结果   @Overridepublic BinaryOperator<List<T>> combiner() {return (list1,list2)->{list1.addAll(list2);return list1;};}//对结果进行处理//这里直接返回@Overridepublic Function<List<T>, List<T>> finisher() {
//      return e->e;return Function.identity();}//设置属性@Overridepublic Set<Characteristics> characteristics() {return Collections.unmodifiableSet(EnumSet.of(Characteristics.CONCURRENT,Characteristics.IDENTITY_FINISH));}}//class EasyToMapCollector<T,K, V> implements Collector<T, Map<K, V>, Map<K, V>> {private Function<? super T, ? extends K> keyMapper;private Function<? super T, ? extends V> valueMapper;private BinaryOperator<Map<K, V>> binOp;//2个参数的,只能是顺序流public EasyToMapCollector(Function<? super T, ? extends K> keyMapper,Function<? super T, ? extends V> valueMapper) {
//      this.keyMapper = keyMapper;
//      this.valueMapper = valueMapper;this(keyMapper,valueMapper,null);}public EasyToMapCollector(Function<? super T, ? extends K> keyMapper,Function<? super T, ? extends V> valueMapper, BinaryOperator<Map<K, V>> binOp) {this.keyMapper = keyMapper;this.valueMapper = valueMapper;this.binOp=binOp;}@Overridepublic BiConsumer<Map<K, V>, T> accumulator() {return (map, element) -> map.put(keyMapper.apply(element), valueMapper.apply(element));//如果同一个key对应多个value,想让key匹配那个部位null的value,可以用
//      return (map,element)->map.putIfAbsent(keyMapper.apply(element), valueMapper.apply(element));}@Overridepublic Supplier<Map<K, V>> supplier() {return HashMap::new;}@Overridepublic BinaryOperator<Map<K, V>> combiner() {
//      return null;//如果不用这个方法最好是抛异常
//      throw new UnsupportedOperationException();return binOp;}@Overridepublic Function<Map<K, V>, Map<K, V>> finisher() {return Function.identity();}@Overridepublic Set<Characteristics> characteristics() {return Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH));}}final class  MyToMap {public  static  <T,K, V> Collector<T, ?, Map<K, V>> toMap(Function<T, K> f1, Function<T, V> f2) {return new EasyToMapCollector<T,K,V>(f1, f2);}public  static  <T,K, V> Collector<T, ?, Map<K, V>> toMap(Function<T, K> f1, Function<T, V> f2,BinaryOperator<Map<K, V>> binOp) {return new EasyToMapCollector<T,K,V>(f1, f2,binOp);}}

这篇关于java8 自定义收集器Collector的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1018772

相关文章

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2