博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Stream流与Lambda表达式(四) 自定义收集器
阅读量:6582 次
发布时间:2019-06-24

本文共 12482 字,大约阅读时间需要 41 分钟。

一、自定义SetCustomCollector收集器

package com.java.design.Stream.CustomCollector;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;/** * @author 陈杨 *///  将List集合转换为Set集合 存放相同元素public class SetCustomCollector
implements Collector
, Set
> { @Override public Supplier
> supplier() { System.out.println("supplier invoked!"); // return TreeSet::new; return HashSet::new; } @Override public BiConsumer
, T> accumulator() { System.out.println("accumulator invoked!"); return Set
::add; } @Override public BinaryOperator
> combiner() { System.out.println("combiner invoked!"); return (first, last) -> { first.addAll(last); return first; }; } @Override public Function
, Set
> finisher() { System.out.println("finisher invoked!"); return Function.identity(); } @Override public Set
characteristics() { System.out.println("characteristics invoked!"); return Collections.unmodifiableSet(EnumSet.of (Characteristics.IDENTITY_FINISH, Characteristics.UNORDERED)); // return Collections.unmodifiableSet(EnumSet.of(Characteristics.UNORDERED)); }}

二、自定义StudentCustomCollector收集器

package com.java.design.Stream.CustomCollector;import com.java.design.java8.entity.Student;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;/** * @author 陈杨 */// 将学生对象 按照HashMap
存放 sid studentpublic class StudentCustomCollector implements Collector
, Map
> { @Override public Supplier
> supplier() { System.out.println("supplier invoked!"); return ArrayList::new; } @Override public BiConsumer
, Student> accumulator() { System.out.println("accumulator invoked!"); return (list, student) -> { System.out.println("accumulator:" + Thread.currentThread().getName()); list.add(student); }; } @Override public BinaryOperator
> combiner() { System.out.println("combiner invoked!"); return (first, last) -> { first.addAll(last); return first; }; } @Override public Function
, Map
> finisher() { System.out.println("finisher invoked!"); return list -> { Map
map = new HashMap<>(); list.forEach(student -> map.put(student.getId(), student)); return map; }; } @Override public Set
characteristics() { System.out.println("Characteristics invoked!"); return Collections.unmodifiableSet(EnumSet.of(Characteristics.CONCURRENT)); } // Characteristics.IDENTITY_FINISH 从中间容器数据类型 转换为 结果类型 数据类型一致 // 若不一致 抛出类型转换异常 finisher对中间容器数据-->结果类型 进行强制类型转换 // Characteristics.CONCURRENT 多个线程同时操作同一个容器 --> 并行 // Indicates that this collector is
concurrent, meaning that // the result container can support the accumulator function being // called concurrently with the same result container from multiple threads. // parallelStream (多线程)并行流 操作 多个结果容器 --> 执行combiner // Characteristics.CONCURRENT + parallelStream 结果容器只有1个 ---> 不执行 combiner // ConcurrentModificationException 并发修改异常 // 注意:并行情况下 累加器对结果容器执行单一操作 // 不要在累加器返回的函数式接口实例中做额外的操作 // 不能打印集合类容 同时向集合里添加新元素 // This exception may be thrown by methods that have detected concurrent // modification of an object when such modification is not permissible}

三、SetCustomCollectorTest测试

package com.java.design.java8.Stream.CustomCollector;import com.java.design.Stream.CustomCollector.SetCustomCollector;import com.java.design.java8.entity.Student;import com.java.design.java8.entity.Students;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.util.List;import java.util.Set;/** * @author 陈杨 */@SpringBootTest@RunWith(SpringRunner.class)public class SetCustomCollectorTest {    private List
students; @Before public void init() { students = new Students().init(); } @Test public void testSetCustomCollector() { Set
set = students.stream().collect(new SetCustomCollector<>()); System.out.println(set); } /*public static
TerminalOp
makeRef(Collector
collector) { Supplier
supplier = Objects.requireNonNull(collector).supplier(); BiConsumer
accumulator = collector.accumulator(); BinaryOperator
combiner = collector.combiner(); class ReducingSink extends Box implements AccumulatingSink
{ @Override public void begin(long size) { state = supplier.get(); } @Override public void accept(T t) { accumulator.accept(state, t); } @Override public void combine(ReducingSink other) { state = combiner.apply(state, other.state); } } return new ReduceOp
(StreamShape.REFERENCE) { @Override public ReducingSink makeSink() { return new ReducingSink(); } @Override public int getOpFlags() { return collector.characteristics().contains(Collector.Characteristics.UNORDERED) ? StreamOpFlag.NOT_ORDERED : 0; } }; }*/ /*public final
R collect(Collector
collector) { A container; if (isParallel() && (collector.characteristics().contains(Collector.Characteristics.CONCURRENT)) && (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) { container = collector.supplier().get(); BiConsumer
accumulator = collector.accumulator(); forEach(u -> accumulator.accept(container, u)); } else { container = evaluate(ReduceOps.makeRef(collector)); } return collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH) ? (R) container : collector.finisher().apply(container); }*/ // 执行流程 方法调用顺序 // container = evaluate(ReduceOps.makeRef(collector)); // Supplier
supplier = Objects.requireNonNull(collector).supplier(); // BiConsumer
accumulator = collector.accumulator(); // BinaryOperator
combiner = collector.combiner(); // return collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)是否有序 // return collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)是否包含IDENTITY_FINISH // ? (R) container 注意强制类型转换 (中间类型 与 返回结果类型) // 注意强制类型转换 /*CollectorImpl(Supplier supplier, BiConsumer
accumulator, BinaryOperator
combiner, Set
characteristics) { this(supplier, accumulator, combiner, castingIdentity(), characteristics); } @SuppressWarnings("unchecked") private static
Function
castingIdentity() { return i -> (R) i; }*/ // EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.UNORDERED) // 包含 IDENTITY_FINISH 打印结果 // supplier invoked! // accumulator invoked! // combiner invoked! // characteristics invoked! // characteristics invoked! // Set
集合对象 // EnumSet.of(Characteristics.UNORDERED) // 不包含 IDENTITY_FINISH 打印结果 // supplier invoked! // accumulator invoked! // combiner invoked! // characteristics invoked! // characteristics invoked! // finisher invoked! // Set
集合对象}

四、StudentCustomCollectorTest测试

package com.java.design.java8.Stream.CustomCollector;import com.java.design.Stream.CustomCollector.StudentCustomCollector;import com.java.design.java8.entity.Student;import com.java.design.java8.entity.Students;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.util.List;import java.util.Map;/** * @author 陈杨 */@SpringBootTest@RunWith(SpringRunner.class)public class StudentCustomCollectorTest {    private List
students; @Before public void init() { students = new Students().init(); } @Test public void testStudentCustomCollectorTest() { System.out.println("单线程"); Map
sequentialMap = students.stream().collect(new StudentCustomCollector()); System.out.println("串行流执行效果:\n---------------------------------------\n"+sequentialMap); System.out.println("---------------------------------------\n"); System.out.println("多线程"); Map
parallelMap = students.parallelStream().collect(new StudentCustomCollector()); System.out.println("并行流执行效果:\n---------------------------------------\n"+parallelMap); System.out.println("---------------------------------------\n"); }}

五、测试结果

SetCustomCollectorTest测试结果 .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v2.1.2.RELEASE)2019-02-20 17:14:45.547  INFO 3260 --- [           main] c.j.d.j.S.C.SetCustomCollectorTest       : Starting SetCustomCollectorTest on DESKTOP-87RMBG4 with PID 3260 (started by 46250 in E:\IdeaProjects\design)2019-02-20 17:14:45.548  INFO 3260 --- [           main] c.j.d.j.S.C.SetCustomCollectorTest       : No active profile set, falling back to default profiles: default2019-02-20 17:14:46.055  INFO 3260 --- [           main] c.j.d.j.S.C.SetCustomCollectorTest       : Started SetCustomCollectorTest in 0.686 seconds (JVM running for 1.43)supplier      invoked!accumulator      invoked!combiner      invoked!characteristics      invoked!characteristics      invoked![Student(id=4, name=Yuuki, sex=Female, age=15, addr=Alfheim Online, salary=9.99999999E8), Student(id=3, name=Sinon, sex=Female, age=16, addr=Gun Gale Online, salary=9.99999999E8), Student(id=5, name=Alice, sex=Female, age=14, addr=Alicization, salary=9.99999999E8), Student(id=2, name=Asuna, sex=Female, age=17, addr=Sword Art Online, salary=9.99999999E8), Student(id=1, name=Kirito, sex=Male, age=18, addr=Sword Art Online, salary=9.99999999E8)]
StudentCustomCollectorTest测试 .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v2.1.2.RELEASE)2019-02-20 17:15:52.817  INFO 3292 --- [           main] c.j.d.j.S.C.StudentCustomCollectorTest   : Starting StudentCustomCollectorTest on DESKTOP-87RMBG4 with PID 3292 (started by 46250 in E:\IdeaProjects\design)2019-02-20 17:15:52.818  INFO 3292 --- [           main] c.j.d.j.S.C.StudentCustomCollectorTest   : No active profile set, falling back to default profiles: default2019-02-20 17:15:53.354  INFO 3292 --- [           main] c.j.d.j.S.C.StudentCustomCollectorTest   : Started StudentCustomCollectorTest in 0.745 seconds (JVM running for 1.439)单线程supplier      invoked!accumulator      invoked!combiner      invoked!Characteristics      invoked!accumulator:mainaccumulator:mainaccumulator:mainaccumulator:mainaccumulator:mainCharacteristics      invoked!finisher      invoked!串行流执行效果:---------------------------------------{1=Student(id=1, name=Kirito, sex=Male, age=18, addr=Sword Art Online, salary=9.99999999E8), 2=Student(id=2, name=Asuna, sex=Female, age=17, addr=Sword Art Online, salary=9.99999999E8), 3=Student(id=3, name=Sinon, sex=Female, age=16, addr=Gun Gale Online, salary=9.99999999E8), 4=Student(id=4, name=Yuuki, sex=Female, age=15, addr=Alfheim Online, salary=9.99999999E8), 5=Student(id=5, name=Alice, sex=Female, age=14, addr=Alicization, salary=9.99999999E8)}---------------------------------------多线程Characteristics      invoked!Characteristics      invoked!supplier      invoked!accumulator      invoked!combiner      invoked!Characteristics      invoked!accumulator:mainaccumulator:ForkJoinPool.commonPool-worker-5accumulator:ForkJoinPool.commonPool-worker-5accumulator:ForkJoinPool.commonPool-worker-3accumulator:mainCharacteristics      invoked!finisher      invoked!并行流执行效果:---------------------------------------{1=Student(id=1, name=Kirito, sex=Male, age=18, addr=Sword Art Online, salary=9.99999999E8), 2=Student(id=2, name=Asuna, sex=Female, age=17, addr=Sword Art Online, salary=9.99999999E8), 3=Student(id=3, name=Sinon, sex=Female, age=16, addr=Gun Gale Online, salary=9.99999999E8), 4=Student(id=4, name=Yuuki, sex=Female, age=15, addr=Alfheim Online, salary=9.99999999E8), 5=Student(id=5, name=Alice, sex=Female, age=14, addr=Alicization, salary=9.99999999E8)}---------------------------------------

转载地址:http://cnino.baihongyu.com/

你可能感兴趣的文章
深度解析Istio系列之安全模块篇
查看>>
Linux 系统 审计
查看>>
JS -------------------设置弹出框位置屏幕的中间
查看>>
性能测试 vbs使用(一)
查看>>
1.2 linux哲学思想
查看>>
jQuery基础
查看>>
BZOJ5312:冒险——题解
查看>>
echarts,两点连线,中间断裂
查看>>
samba简易配置
查看>>
庆祝在CNBlogs开博!
查看>>
javascript reverse string
查看>>
南阳oj 题目6 喷水装置(一)
查看>>
运筹学上机实验 - 单纯形方法的两阶段法
查看>>
CF294C Shaass and Lights
查看>>
oracle 11g 报错记录
查看>>
文件状态是否变化
查看>>
MongoDB的副本集Replica Set
查看>>
Maven项目中的配置文件找不到以及打包问题
查看>>
面向对象
查看>>
HDU 1058 Humble Numbers
查看>>