当前位置: 首页 > news >正文

Java lambda表达式如何自定义一个toList Collector

匿名类:

package l8;import java.util.*;
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.Collectors;public class CollectorToList2 {public static void main(String[] args) {/***<T> – the type of input elements to the reduction operation<A> – the mutable accumulation type of the reduction operation (often hidden as an implementation detail)<R> – the result type of the reduction operation  最终返回结果的类型*/Collector<Integer, List<Integer>, List<String>> toList = new Collector<Integer, List<Integer>, List<String>>() {// 初始一个容器,用于做为累加的容器@Overridepublic Supplier<List<Integer>> supplier() {return () -> new ArrayList<>();}/*** 元素累加* @return*/@Overridepublic BiConsumer<List<Integer>, Integer> accumulator() {return List::add;}/*** 将多个容器进行合并(应该是在并行Stream时使用的)* @return*/@Overridepublic BinaryOperator<List<Integer>> combiner() {return (a, b) -> {System.out.println("combiner call");a.addAll(b);return a;};}/*** 最终类型转换* @return*/@Overridepublic Function<List<Integer>, List<String>> finisher() {return list -> list.stream().map(e -> e + "").collect(Collectors.toList());}@Overridepublic Set<Characteristics> characteristics() {return Collections.singleton(Characteristics.UNORDERED);}};List<String> collect = Arrays.asList(1, 2, 3).stream().collect(toList);System.out.println(collect);collect = Arrays.asList(1, 2, 3).parallelStream().collect(toList);System.out.println(collect);}
}
javascript:void(0)

Combiner:

应用:

优化初始容器的容量:


/*** <T> – the type of input elements to the reduction operation* <A> – the mutable accumulation type of the reduction operation (often hidden as an implementation detail)* <R> – the result type of the reduction operation  最终返回结果的类型*/
class ToListWithInitialCapacity implements Collector<Integer, List<Integer>, List<String>> {private int initialCapacity;public ToListWithInitialCapacity(int initialCapacity) {this.initialCapacity = initialCapacity;}// 初始一个容器,用于做为累加的容器@Overridepublic Supplier<List<Integer>> supplier() {return () -> new ArrayList<>(initialCapacity);}/*** 元素累加** @return*/@Overridepublic BiConsumer<List<Integer>, Integer> accumulator() {return List::add;}/*** 将多个容器进行合并(应该是在并行Stream时使用的)** @return*/@Overridepublic BinaryOperator<List<Integer>> combiner() {return (a, b) -> {System.out.println("combiner call");a.addAll(b);return a;};}/*** 最终类型转换** @return*/@Overridepublic Function<List<Integer>, List<String>> finisher() {return list -> list.stream().map(e -> e + "").collect(Collectors.toList());}@Overridepublic Set<Characteristics> characteristics() {return Collections.singleton(Characteristics.UNORDERED);}
}

Jdk toList默认实现:

    /*** Returns a {@code Collector} that accumulates the input elements into a* new {@code List}. There are no guarantees on the type, mutability,* serializability, or thread-safety of the {@code List} returned; if more* control over the returned {@code List} is required, use {@link #toCollection(Supplier)}.** @param <T> the type of the input elements* @return a {@code Collector} which collects all the input elements into a* {@code List}, in encounter order*/public static <T>Collector<T, ?, List<T>> toList() {return new CollectorImpl<>(ArrayList::new, List::add,(left, right) -> { left.addAll(right); return left; },CH_ID);}

http://www.lryc.cn/news/278539.html

相关文章:

  • 【hcie-cloud】【18】华为云Stack灾备服务介绍【容灾解决方案介绍、灾备方案架构介绍、管理组件灾备方案介绍、高阶云服务容灾简介、缩略词】【下】
  • linux建立软链接——以matlab为例
  • ubuntu20固定串口名称
  • 扩散模型(二)——DDIM学习笔记-大白话推导
  • 【软件测试作业_TPshop商城】农业工程学院-测试需求分析与测试计划+自动化+性能+测试用例+报告软件缺陷+测试计划+单元测试+系统测试
  • 屏幕截图编辑工具Snagit中文
  • 12GoF之代理模式
  • Unity中URP下实现能量罩(扭曲流光花纹)
  • 南京银行高管上新:“70后董事长”谢宁将上任,能否及时救场?
  • K8S容器编排基本使用
  • PyTorch 各种池化层函数全览与用法演示
  • Redis:原理速成+项目实战——Redis实战7(优惠券秒杀+细节解决超卖、一人一单问题)
  • 【刷题笔记3】
  • YOLOv8优化策略:轻量化改进 | 华为Ghostnet,超越谷歌MobileNet | CVPR2020
  • 格雷希尔G65系列快速接头满足汽车减震器的气压、油压测试要求
  • php中常用的几个安全函数
  • 【K8S 云原生】Kurbernets集群的调度策略
  • vue-office 支持多种文件(docx、excel、pdf)预览的vue组件库
  • 如何使用GaussDB创建脱敏策略(MASKING POLICY)
  • 【Golang map并发报错】panic: assignment to entry in nil map
  • 【GO语言依赖】Go语言依赖管理简述
  • 论文阅读记录SuMa SuMa++
  • 性能分析与调优: Linux 内存观测工具
  • 【ARM 嵌入式 编译系列 3.4 -- 查看所依赖库文件的路径 详细介绍】
  • 分布式锁3: zk实现分布式锁3 使用临时顺序节点+watch监听实现阻塞锁
  • google drive api
  • 3_代理模式(动态代理JDK原生和CGLib)
  • Linux的权限(1)
  • 数据安全保障的具体措施有哪些
  • 浅谈标签及应用场景