Java8 Stream Collectors收集器使用方法解析

时间:2021-05-20

Collectors.toMap:

Student studentA = new Student("20190001","小明"); Student studentB = new Student("20190002","小红"); Student studentC = new Student("20190003","小丁"); //Function.identity() 获取这个对象本身,那么结果就是Map<String,Student> 即 id->student //串行收集 Stream.of(studentA,studentB,studentC) .collect(Collectors.toMap(Student::getId,Function.identity())); //并发收集 Stream.of(studentA,studentB,studentC) .parallel() .collect(Collectors.toConcurrentMap(Student::getId,Function.identity())); //================================================================================ //Map<String,String> 即 id->name //串行收集 Stream.of(studentA,studentB,studentC) .collect(Collectors.toMap(Student::getId,Student::getName)); //并发收集 Stream.of(studentA,studentB,studentC) .parallel() .collect(Collectors.toConcurrentMap(Student::getId,Student::getName));

那么如果key重复的该怎么处理?这里我们假设有两个id相同Student,如果他们id相同,在转成Map的时候,取name大一个,小的将会被丢弃。

//Map<String,Student> //maxby ==sordBy 倒序 minBy or .maxBy(Comparator.comparing(User::getName).reversed()))); Stream.of(studentA, studentB, studentC) .collect(Collectors .toMap(Student::getId, Function.identity(), BinaryOperator .maxBy(Comparator.comparing(Student::getName)))); //可能上面比较复杂,这编写一个命令式 //Map<String,Student> Stream.of(studentA, studentB, studentC) .collect(Collectors .toMap(Student::getId, Function.identity(), (s1, s2) -> { //这里使用compareTo 方法 s1>s2 会返回1,s1==s2 返回0 ,否则返回-1 if (((Student) s1).name.compareTo(((Student) s2).name) < -1) { return s2; } else { return s1; } }));

如果不想使用默认的HashMap 或者 ConcurrentHashMap , 第三个重载方法还可以使用自定义的Map对象(Map工厂)。

//自定义LinkedHashMap //Map<String,Student> Stream.of(studentA, studentB, studentC) .collect(Collectors .toMap(Student::getId, Function.identity(), BinaryOperator .maxBy(Comparator.comparing(Student::getName)), LinkedHashMap::new));

Collectors.groupingBy()和Collectors.groupingByConcurrent(),这两者区别也仅是单线程和多线程的使用场景。为什么要groupingBy归类为前后处理呢?groupingBy 是在数据收集前分组的,再将分好组的数据传递给下游的收集器。

这是 groupingBy最长的参数的函数classifier 是分类器,mapFactory map的工厂,downstream下游的收集器,正是downstream 的存在,可以在数据传递个下游之前做很多的骚操作。

public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)

示例:这里将一组数整型数分为正数、负数、零,groupingByConcurrent()的参数也是跟它一样的就不举例了。

//Map<String,List<Integer>> Stream.of(-6, -7, -8, -9, 1, 2, 3, 4, 5, 6) .collect(Collectors.groupingBy(integer -> { if (integer < 0) { return "小于"; } else if (integer == 0) { return "等于"; } else { return "大于"; } })); //Map<String,Set<Integer>> //自定义下游收集器 Stream.of(-6, -7, -8, -9, 1, 2, 3, 4, 5, 6) .collect(Collectors.groupingBy(integer -> { if (integer < 0) { return "小于"; } else if (integer == 0) { return "等于"; } else { return "大于"; } },Collectors.toSet())); //Map<String,Set<Integer>> //自定义map容器 和 下游收集器 Stream.of(-6, -7, -8, -9, 1, 2, 3, 4, 5, 6) .collect(Collectors.groupingBy(integer -> { if (integer < 0) { return "小于"; } else if (integer == 0) { return "等于"; } else { return "大于"; } },LinkedHashMap::new,Collectors.toSet()));

Collectors.partitioningBy()

字面意思话就叫分区好了,但是partitioningBy最多只能将数据分为两部分,因为partitioningBy分区的依据Predicate,而Predicate只会有true 和false 两种结果,所有partitioningBy最多只能将数据分为两组。partitioningBy除了分类器与groupingBy 不一样外,其他的参数都相同。

示例:

//Map<Boolean,List<Integer>> Stream.of(0,1,0,1) .collect(Collectors.partitioningBy(integer -> integer==0)); //Map<Boolean,Set<Integer>> //自定义下游收集器 Stream.of(0,1,0,1) .collect(Collectors.partitioningBy(integer -> integer==0,Collectors.toSet()));

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章