Java中如何使用Stream Collectors收集器?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
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()));
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。