一、Collector接口解读:      

Collector接口解读:

 public interface Collector<T, A, R> {
Supplier<A> supplier();
BiConsumer<A, T> accumulator();
BinaryOperator<A> combiner();
Function<A, R> finisher();
Set<Characteristics> characteristics();
}

Collector<T, A, R>
T: stream中的元素类型;
A:累加器的类型,可以想象成一个容器。比如T要累加,转化为List<T>,A就是List类型。
R:最终返回值类型
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.

二、自定义Collector,看看是怎么实现的

仿照Collectors.toList,自定义实现一个Collector的接口:

 package com.cy.java8;

 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; public class ToListCollector<T> implements Collector<T, List<T>, List<T>> { private void log(final String s){
System.out.println(Thread.currentThread().getName() + "-" + s);
} @Override
public Supplier<List<T>> supplier() {
log("supplier");
return ArrayList::new;
} @Override
public BiConsumer<List<T>, T> accumulator() {
log("accumulator");
return (list, v) -> list.add(v);
} @Override
public BinaryOperator<List<T>> combiner() {
log("combiner");
return (left, right) -> {
left.addAll(right);
return left;
};
} @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(Collector.Characteristics.IDENTITY_FINISH,
Collector.Characteristics.CONCURRENT));
}
}

测试执行:

 package com.cy.java8;

 import java.util.Arrays;
import java.util.List;
import java.util.stream.Collector; public class CustomCollectorAction { public static void main(String[] args) {
Collector<String, List<String>, List<String>> collector = new ToListCollector<>(); String[] array = new String[]{"Java 8", "Hello", "Collector", "Custom", "Stream"}; List<String> list1 = Arrays.stream(array).filter(s -> s.length()>5).collect(collector);
System.out.println(list1); List<String> list2 = Arrays.stream(array).parallel().filter(s -> s.length()>5).collect(collector);
System.out.println(list2);
} }

console:

main-supplier
main-accumulator
main-combiner
main-characteristics
main-characteristics
[Java 8, Collector, Custom, Stream]
main-characteristics
main-characteristics
main-supplier
main-accumulator
main-combiner
main-characteristics
main-characteristics
[Java 8, Collector, Custom, Stream]

  

---

Collector解读以及自定义的更多相关文章

  1. 【pytest官方文档】解读- 如何自定义mark标记,并将测试用例的数据传递给fixture函数

    在之前的分享中,我们知道可以使用yield或者return关键字把fixture函数里的值传递给test函数. 这种方法很实用,比如我在fixture函数里向数据库里插入必要的测试数据,那我就可以把相 ...

  2. Lucene 搜索功能

    搜索过程 图解: 主要 API: IndexSearcher:    //所有搜索都通过 IndexSearcher 进行,他们将调用该类中重载的 search() 方法 Query:         ...

  3. net开发框架never

    [一] 摘要 never是纯c#语言开发的一个框架,同时可在netcore下运行. 该框架github地址:https://github.com/shelldudu/never 同时,配合never_ ...

  4. 『动善时』JMeter基础 — 61、使用JMeter监控服务器

    目录 1.监控插件安装 2.启动监控服务 3.使用JMeter监控服务器 (1)测试计划内包含的元件 (2)HTTP请求界面内容 (3)配置jp@gc-PerfMon Metrics Collecto ...

  5. 解读ASP.NET 5 & MVC6系列(16):自定义View视图文件查找逻辑

    之前MVC5和之前的版本中,我们要想对View文件的路径进行控制的话,则必须要对IViewEngine接口的FindPartialView或FindView方法进行重写,所有的视图引擎都继承于该IVi ...

  6. 怎么在java中创建一个自定义的collector

    目录 简介 Collector介绍 自定义Collector 总结 怎么在java中创建一个自定义的collector 简介 在之前的java collectors文章里面,我们讲到了stream的c ...

  7. Java Stream 自定义Collector

    Collector的使用 使用Java Stream流操作数据时,经常会用到各种Collector收集器来进行数据收集. 这里便深入了解一点去了解Collector的工作原理和如何自定义Collect ...

  8. 如何自定义一个Collector

    Collectors类提供了很多方便的方法,假如现有的实现不能满足需求,我们如何自定义一个Collector呢?   Collector接口提供了一个of方法,调用该方法就可以实现定制Collecto ...

  9. 详细解读Volley(四)—— 自定义Request

    Volley中提供了几个Request,如果我们有特殊的需求,完全可以自定义Request的,自定义Request自然要继承Request,那么本篇就教大家来一步一步地定义一个自己的Request类. ...

随机推荐

  1. node.js安装后输入“node -v”提示'node' 不是内部或外部命令,也不是可运行的程序的解决方法

    换个电脑,重新搭配环境的时候遇到的问题.node.js已经在官网进行下载安装了,但是VScode里面显示不是内部的命令,也不是可运行的程序 但是在cmd控制台还是能查到的 借助网上的方法进行了测试和调 ...

  2. intellij 编译 springmvc+hibernate+spring+maven 找不到hbm.xml映射文件

    1. 错误信息 Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException ...

  3. 一、doT.js使用笔记

    一.赋值https://www.jianshu.com/p/19156f9fac1e <!DOCTYPE html> <html> <head> <meta ...

  4. 重置grafana密码

    [root@host~]# sqlite3 /var/lib/grafana/grafana.db SQLite version 3.7.17 2013-05-20 00:56:22 Enter &q ...

  5. Luogu P3886 [JLOI2009]神秘的生物 最小表示法,轮廓线DP,插头DP,动态规划

    亲手写掉的第一道最小表示法!哈哈哈太开心啦~ 不同于以往的几个插头\(dp\),这个题目的轮廓线是周围的一圈\(n\)个格子.而其所谓"插头"也变成了相邻格子的所属连通分量编号,并 ...

  6. NSMutableArray修改替换某个位置的元素

    使用NSMutableArray的replaceObjectAtIndex方法.

  7. SonarQube 7.7默认数据库连接方法

    SonarQube7.7默认数据库为H2 embbed数据库 连接字符串:jdbc:h2:tcp://localhost:9092/sonar 用户名密码都为空

  8. 批量下载文件asp.net

    一.实现步骤 在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹.然后调用 RAR程序,对临时文件夹进行压缩,然后输出到 ...

  9. BZOJ 4773: 负环 倍增Floyd

    现在看来这道题就非常好理解了. 可以将问题转化为求两点间经过 $k$ 个点的路径最小值,然后枚举剩余的那一个点即可. #include <cstdio> #include <cstr ...

  10. POJ 2155 Matrix (树状数组 && 区间计数)

    题意 : 给出一个N*N的矩阵, 矩阵只有可能包含0或1, 一开始则全部是0.对于矩阵可以进行两种操作, 第一种是输入 C x1 y1 x2 y2 表示, 对以(x1, y1)为左上角, 以(x2, ...