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五子棋之坐标校正

上篇针对了Java项目中的解构思维,在这篇内容中我们不妨从整体项目中拆解拿出一个非常重要的五子棋逻辑实现:坐标校正,我们如何使漫无目的鼠标点击变得有序化和可控化呢? 目录 一、从鼠标监听到获取坐标 1.MouseListener和MouseAdapter 2.mousePressed方法 二、坐标校正的具体实现方法 1.关于fillOval方法 2.坐标获取 3.坐标转换 4.坐

Spring Cloud:构建分布式系统的利器

引言 在当今的云计算和微服务架构时代,构建高效、可靠的分布式系统成为软件开发的重要任务。Spring Cloud 提供了一套完整的解决方案,帮助开发者快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器等)。本文将探讨 Spring Cloud 的定义、核心组件、应用场景以及未来的发展趋势。 什么是 Spring Cloud Spring Cloud 是一个基于 Spring

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

java8的新特性之一(Java Lambda表达式)

1:Java8的新特性 Lambda 表达式: 允许以更简洁的方式表示匿名函数(或称为闭包)。可以将Lambda表达式作为参数传递给方法或赋值给函数式接口类型的变量。 Stream API: 提供了一种处理集合数据的流式处理方式,支持函数式编程风格。 允许以声明性方式处理数据集合(如List、Set等)。提供了一系列操作,如map、filter、reduce等,以支持复杂的查询和转

Java面试八股之怎么通过Java程序判断JVM是32位还是64位

怎么通过Java程序判断JVM是32位还是64位 可以通过Java程序内部检查系统属性来判断当前运行的JVM是32位还是64位。以下是一个简单的方法: public class JvmBitCheck {public static void main(String[] args) {String arch = System.getProperty("os.arch");String dataM

详细分析Springmvc中的@ModelAttribute基本知识(附Demo)

目录 前言1. 注解用法1.1 方法参数1.2 方法1.3 类 2. 注解场景2.1 表单参数2.2 AJAX请求2.3 文件上传 3. 实战4. 总结 前言 将请求参数绑定到模型对象上,或者在请求处理之前添加模型属性 可以在方法参数、方法或者类上使用 一般适用这几种场景: 表单处理:通过 @ModelAttribute 将表单数据绑定到模型对象上预处理逻辑:在请求处理之前

eclipse运行springboot项目,找不到主类

解决办法尝试了很多种,下载sts压缩包行不通。最后解决办法如图: help--->Eclipse Marketplace--->Popular--->找到Spring Tools 3---->Installed。

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

Java面试题:通过实例说明内连接、左外连接和右外连接的区别

在 SQL 中,连接(JOIN)用于在多个表之间组合行。最常用的连接类型是内连接(INNER JOIN)、左外连接(LEFT OUTER JOIN)和右外连接(RIGHT OUTER JOIN)。它们的主要区别在于它们如何处理表之间的匹配和不匹配行。下面是每种连接的详细说明和示例。 表示例 假设有两个表:Customers 和 Orders。 Customers CustomerIDCus

22.手绘Spring DI运行时序图

1.依赖注入发生的时间 当Spring loC容器完成了 Bean定义资源的定位、载入和解析注册以后,loC容器中已经管理类Bean 定义的相关数据,但是此时loC容器还没有对所管理的Bean进行依赖注入,依赖注入在以下两种情况 发生: 、用户第一次调用getBean()方法时,loC容器触发依赖注入。 、当用户在配置文件中将<bean>元素配置了 lazy-init二false属性,即让