原创转载请注明出处:http://agilestyle.iteye.com/blog/2425967
The Collector Interface
public interface Collector<T, A, R> { Supplier<A> supplier(); BiConsumer<A, T> accumulator(); BinaryOperator<A> combiner(); Function<A, R> finisher(); Set<Collector.Characteristics> characteristics(); }
Note:
T is the generic type of the items in the stream to be collected.
A is the type of the accumulator, the object on which the partial result will be accumulated during the collection process.
R is the type of the object (typically, but not always, the collection) resulting from the collect operation.
实现一个自定义的 ToListCollector<T>
Making a new result container: the supplier method
@Override public Supplier<List<T>> supplier() { return ArrayList::new; }
Adding an element to a result container: the accumulator method
@Override public BiConsumer<List<T>, T> accumulator() { return List::add; }
Applying the final transformation to the result container: the finisher method
@Override public Function<List<T>, List<T>> finisher() { return Function.identity(); }
Logical steps of the sequential reduction process
Merging two result containers: the combiner method
@Override public BinaryOperator<List<T>> combiner() { return (list1, list2) -> { list1.addAll(list2); return list1; }; }
Parallelizing the reduction process using the combiner method
Characteristics method
characteristics, returns an immutable set of Characteristics, defining the behavior of the collector—in particular providing hints about whether the stream can be reduced in parallel and which optimizations are valid when doing so. Characteristics is an enumeration containing three items:
- UNORDERED—The result of the reduction isn’t affected by the order in which the items in the stream are traversed and accumulated.
- CONCURRENT—The accumulator function can be called concurrently from multiple threads, and then this collector can perform a parallel reduction of the stream. If the collector isn’t also flagged as UNORDERED, it can perform a parallel reduction only when it’s applied to an unordered data source.
- IDENTITY_FINISH—This indicates the function returned by the finisher method is the identity one, and its application can be omitted. In this case, the accumulator object is directly used as the final result of the reduction process. This also implies that it’s safe to do an unchecked cast from the accumulator A to the result R.
@Override public Set<Characteristics> characteristics() { return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT)); }
Putting them all together
package org.fool.java8.collector; import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; import java.util.List; 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; public class ToListCollector<T> implements Collector<T, List<T>, List<T>> { private void log(String log) { System.out.println(Thread.currentThread().getName() + "-" + log); } @Override public Supplier<List<T>> supplier() { log("supplier"); return ArrayList::new; } @Override public BiConsumer<List<T>, T> accumulator() { log("accumulator"); return List::add; } @Override public BinaryOperator<List<T>> combiner() { log("combiner"); return (list1, list2) -> { list1.addAll(list2); return list1; }; } @Override public Function<List<T>, List<T>> finisher() { log("finisher"); return Function.identity(); } @Override public Set<Characteristics> characteristics() { log("characteristics"); //return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT)); return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH)); } }
Test.java
package org.fool.java8.collector; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Collector; public class Test { public static void main(String[] args) { Collector<String, List<String>, List<String>> collector = new ToListCollector<>(); String[] strings = {"Hello", "World", "Java 8", "Lambda", "Collector"}; Optional.of(Arrays.stream(strings).filter(s -> s.length() > 5).collect(collector)).ifPresent(System.out::println); } }
Console Output
main-supplier main-accumulator main-combiner main-characteristics main-characteristics [Java 8, Lambda, Collector]
Reference
Manning.Java.8.in.Action.Lambdas.Streams.and.functional-style.programming
相关推荐
《PyPI官网下载:探索collectors-channel-cli-1.1.0.tar.gz的分布式云原生世界》 PyPI(Python Package Index)是Python开发者的重要资源库,它为全球的Python爱好者提供了丰富的开源库和模块。当我们看到"PyPI ...
Create React App入门该项目是通过引导的。可用脚本在项目目录中,可以运行:npm start 在开发模式下运行应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒错误...
Java Stream API Parallel Collectors - 克服标准并行流的限制 Parallel Collectors 是一个工具包,使用 Stream API 简化 Java 中的并行收集处理......但没有标准并行流强加的限制。 list.stream() .collect...
The invokeinterface Instruction Invocation Instructions and Speed Examples of Method Invocation Returning from Methods On the CD-ROM The Resources Page 20 Thread Synchronization Monitors Object ...
windows_exporter 适用于Windows计算机的Prometheus导出器。 收藏家 名称 描述 默认启用 Active Directory域服务 Active Directory联合身份验证服务 缓存指标 CPU使用率 ... “计算机系统”指标(系统属性,cpus数量/...
阿里云MaxCompute数据收集器 ... $ cd aliyun-maxcompute-data-collectors $ mvn clean package -DskipTests=true -Dmaven.javadoc.skip=true 插件包位于每个插件子项目的target 。 用法 请参考的基本用法。
### 关于《MacMillan - Introducing Unix and Linux - Grass Root Series - Collectors Edition》的知识点解析 #### 标题解读: - **书籍名称**:“MacMillan - Introducing Unix and Linux - Grass Root Series - ...
这个方法是Collectors类中的一个静态方法,它实现了Collector接口,用于在流的终止操作中将元素累积到一个Map中。 Collectors.toMap()是Java 8 Stream API中一个非常实用的工具,它简化了将流中的元素收集到Map中的...
Nginx的Graylog内容包,使用来自Graylog Collectors的gelf输入。 该内容包创建了一个GELF输入,供Graylog收集器使用。 提取器将应用于所有收到的消息,以提取nginx访问日志字段和错误日志严重性。 提取的字段以...
在JDK12中,Collectors添加了一个新的teeing方法,该方法可以将两个Collector组合成一个Collector。teeing方法的签名是: ``` public static , R1, R2, R> Collector, ?, R> teeing( Collector, ?, R1> downstream1...
# to install mesos slave collector make -e COLLECTORS_DIR= ~ /tmp mesos_slave # to install all mesos collectors make -e COLLECTORS_DIR= ~ /tmp mesos # to install all collectors make -e COLLECTORS_DIR=...
扫雪机基础图像 这个 docker 文件用于为优秀的套件提供基础镜像。 编译的唯一两个子项目是 scala-stream-collector 和 scala-kinesis-enrich。... /app/snowplow/snowplow/2-collectors/scala-stream-collector/src
在Java 8引入的Stream API中,Collectors类提供了多种收集器(Collector),用于将流(Stream)的元素汇总成各种形式的结果。Collectors.collectingAndThen()是其中一种强大的收集器,它允许开发者在收集过程完成后...
在之前的java collectors文章里面,我们讲到了stream的collect方法可以调用Collectors里面的toList()或者toMap()方法,将结果转换为特定的集合类。 今天我们介绍一下怎么自定义一个Collector。 Collector介绍 我们先...
然而,它同样会导致应用程序的暂停,即“Stop-the-world”事件。通过-XX:+UseParallelGC标志可以启用它。 3. CMS(Concurrent Mark Sweep)Garbage Collector:此垃圾回收器旨在减少应用程序的暂停时间,适合响应...
本文详细介绍了Collectors.toList()方法的基本概念、使用场景、性能优化策略、实际应用案例以及与其他Collector的结合使用。希望能够帮助你在实际开发中更好地利用Java 8的Stream API来处理数据集合。
其中,Collectors.joining()方法是一个专门用于字符串连接的Collector,它提供了一种简洁且高效的方式来将流中的元素合并成一个单一的字符串。本文将深入探讨Collectors.joining()的工作原理、使用场景、性能优化...
这个方法是Collectors类中提供的一个下游收集器(downstream collector),专门用于处理LongStream。它接收一个ToLongFunction作为参数,该函数定义了如何从流元素中提取long类型的数值进行平均值计算。 Collectors....
2. **垃圾回收器调优 (Tune Garbage Collectors)** - 标记-清除收集器算法 (Mark-Sweep Collector Algorithm) - 垃圾回收前 (Before GC) - 垃圾回收后 (After GC) - 优缺点 (Pros and Cons?) - 标记-整理收集器...
在 Java 8 引入的 Stream API 中,java.util.stream.Collectors 类扮演着至关重要的角色。它提供了一种高级的方式来处理集合数据,使得数据聚合和转换操作变得简单而高效。本文将深入探讨 Collectors 类的作用、常用...