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.hqwc.cn/news/341245.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

git: Updates were rejected because the tip of your current branch is behind

一、报错含义 由于本地分支的tip落后远程分支&#xff0c;push操作被拒绝。 二、产生原因 我再本地拉去了新的分支并未同步到远程仓库&#xff0c;在新分支进行开发&#xff0c;由于前几天同步也创建了该分支并同步到了远程仓库&#xff0c;导致我本次push失败 三、解决方…

CES 2024:AI智能大爆发,引领科技新纪元

美国当地时间1月9日&#xff0c;2024年国际消费类电子产品展览会&#xff08;CES&#xff09;如期在内华达州拉斯维加斯举行。作为全球最盛大的科技盛会&#xff0c;每年&#xff0c;来自世界各地的顶尖科技企业都会参加CES&#xff0c;展示他们的最新科技产品和创新&#xff0…

linux磁盘总结

什么是page_cache linux读写磁盘&#xff0c;如果都是采用directIO的话&#xff0c;效率太低&#xff0c;所以我们在读写磁盘上加了一层缓存&#xff0c;page_cache。读的话&#xff0c;如果page_cache有的话&#xff0c;就不用向磁盘发出请求。写的话&#xff0c;也直接写入的…

【开源】基于JAVA语言的康复中心管理系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 普通用户模块2.2 护工模块2.3 管理员模块 三、系统展示四、核心代码4.1 查询康复护理4.2 新增康复训练4.3 查询房间4.4 查询来访4.5 新增用药 五、免责说明 一、摘要 1.1 项目介绍 基于JAVAVueSpringBootMySQL的康复中…

C++ λ表达式

λ表达式提供了函数对象的另一种编程机制。 在 C 11 和更高版本中&#xff0c;Lambda 表达式&#xff08;通常称为 Lambda&#xff09;是一种在被调用的位置或作为参数传递给函数的位置定义匿名函数对象&#xff08;闭包&#xff09;的简便方法。 Lambda 通常用于封装传递给算法…

docker+django+ubuntu服务器 测试质量管理平台部署

1. 首先需要写一个dockerfile就和流水线配置一样 # 使用基础的 Python 镜像作为基础 FROM python:3.8# 设置工作目录 WORKDIR /mydjpro# 将项目的依赖项添加到容器中 COPY requirements.txt /mydjpro/ RUN pip install urllib3 RUN apt-get update && apt-get install…

银河麒麟v10安装前端环境(Node、vue、Electron+vite)

此帖子所提到的所有依赖包都是基于银河麒麟v10真机的arm架构包&#xff0c;如果是在windows上的虚拟机上 把依赖包换成x64的包即可&#xff0c;方法步骤都是一样 一.node安装 原始方法安装&#xff08;建议用第二种nvm方法&#xff0c;因为更简单&#xff09;&#xff1a; 1…

如何在 Windows 中使用Copilot AI

Windows Copilot 是 Windows 中的一个新功能&#xff0c;它可以让你与一个智能助理进行对话&#xff0c;获取信息&#xff0c;执行任务&#xff0c;甚至创造内容。Windows Copilot 使用了 Bing Chat 的技术&#xff0c;它是一个基于 OpenAI 的 GPT-4 模型的聊天机器人。 目录 …

OSG加载STL模型

下载了2个简单stl模型&#xff0c;用基本的加载代码&#xff1b;直接可以加载&#xff1b; 查一点资料&#xff1b; 怎样在OSG中添加支持STL格式的模型文件&#xff1f; 使用OSG时&#xff0c;如果需要导入STL格式的模型文件&#xff0c;需要添加STL插件。 可以通过在代码中调…

Vue基础-搭建Vue运行环境

这篇文章介绍了在Vue.js项目中进行开发环境搭建的关键步骤。包括node.js安装和配置、安装Vue及Vue CLI工具、安装webpack模板、安装vue-router、创建Vue项目等步骤。这篇文章为读者提供了清晰的指南&#xff0c;帮助他们快速搭建Vue.js开发环境&#xff0c;为后续的项目开发奠定…

pyside6 捕捉主窗口关闭后,进行释放相关的资源

import sys from PySide6 import QtGui from PySide6.QtWidgets import QWidget,QApplication,QMessageBoxclass Message(QWidget):def __init__(self):# 如果希望窗口内嵌于其他部件&#xff0c;可添加parent参数super(Message, self).__init__()# 调用初始化方法self.initUI(…

鸿蒙HarmonyOS兼容JS的类Web开发

鸿蒙HarmonyOS兼容JS的类Web开发 文章目录 鸿蒙HarmonyOS兼容JS的类Web开发文件组织目录结构文件访问规则媒体文件格式 js标签配置pageswindow示例 app.js应用生命周期应用对象6 HML语法参考页面结构数据绑定普通事件绑定冒泡事件绑定5捕获事件绑定5列表渲染条件渲染逻辑控制块…