栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

Collectors.toMap:

Student studentA = new Student("20190001","小明");
    Student studentB = new Student("20190002","小红");
    Student studentC = new Student("20190003","小丁");


    //Function.identity() 获取这个对象本身,那么结果就是Map 即 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 即 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  //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
    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
    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 >
  Collector groupingBy(Function classifier,
   Supplier mapFactory,
   Collector downstream) 

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

//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 "大于";
   }
 }));

    //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 "大于";
   }
 },Collectors.toSet()));

    //Map>
    //自定义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>
    Stream.of(0,1,0,1)
 .collect(Collectors.partitioningBy(integer -> integer==0));

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/132722.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号