一、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. python_实现员工信息表

    实现员工信息表 文件存储格式如下:id,name,age,phone,job1,Alex,22,13651054608,IT2,Egon,23,13304320533,Tearcher3,nezha, ...

  2. python 反射、动态导入

    1. 反射 hasattr(obj,'name')            # 判断对象中是否含有字符串形式的方法名或属性名,返回True.False getattr(obj,'name',None)  ...

  3. Java集合及Concurrent并发包总结

    1.集合包 集合包最常用的有Collection和Map两个接口的实现类,Colleciton用于存放多个单对象,Map用于存放Key-Value形式的键值对. Collection中最常用的又分为两 ...

  4. ZROI 19.08.04模拟赛

    传送门 写在前面:为了保护正睿题目版权,这里不放题面,只写题解. "这应该是正睿OI历史上第一次差评破百的比赛." "这说明来正睿集训的人越来越多了." &qu ...

  5. Hive 中的 LEFT SEMI JOIN 与 JOIN ON

    hive 的 join 类型有好几种,其实都是把 MR 中的几种方式都封装实现了,其中 join on.left semi join 算是里边具有代表性,且使用频率较高的 join 方式. 1.联系 ...

  6. scrapy中的selenium

    引入 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生,如果直接使用scrapy对其url发请求,是绝对获取不到那部分动态加载出来的数据值.但是通过观察我们会发现 ...

  7. POJ-1904-King‘s Quest

    链接: https://vjudge.net/problem/POJ-1904 题意: Once upon a time there lived a king and he had N sons. A ...

  8. Tomcat网站上的core和deployer的区别

    8.5.13 Please see the README file for packaging information. It explains what every distribution(分布) ...

  9. (转)electron主线程中通过mainWindow.webContents.send发送事件,渲染线程接收不到

    转自 https://segmentfault.com/q/1010000015599245/ 准备实现的功能: 页面1(渲染进程1)中点击按钮,发送事件给到主进程.主进程成功接收事件后,通过main ...

  10. 在javascript中,如何判断一个被多次encode 的url 已经被decode到原来的格式?

    % 而不能被无限次decodeURIComponent 可以用%来进行判断