• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

java 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

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<em>本文来源gao.dai.ma.com搞@代*码(网$</em>, 3, 4, 5, 6)
        .collect(Collectors.groupingBy(integer -> {
          if (integer < 0) {
            return "小于";
          } else if (integer == 0) {
            return "等于";
          } else {
            return "大于";
          }
        },LinkedHashMap::new,Collectors.toSet()));

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java8 Stream Collectors收集器使用方法解析
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址