当前位置: 移动技术网 > IT编程>开发语言>Java > Java8 Stream Collectors收集器使用方法解析

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

2020年08月07日  | 移动技术网IT编程  | 我要评论
collectors.tomap:student studenta = new student("20190001","小明"); student studentb = new student(

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()));

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网