亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2020年08月06日 15:59:58   作者:lshan  
這篇文章主要介紹了Java8 Stream Collectors收集器使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

Collectors.toMap:

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


    //Function.identity() 獲取這個(gè)對(duì)象本身,那么結(jié)果就是Map<String,Student> 即 id->student
    //串行收集
   Stream.of(studentA,studentB,studentC)
        .collect(Collectors.toMap(Student::getId,Function.identity()));

    //并發(fā)收集
    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));

    //并發(fā)收集
    Stream.of(studentA,studentB,studentC)
        .parallel()
        .collect(Collectors.toConcurrentMap(Student::getId,Student::getName));

那么如果key重復(fù)的該怎么處理?這里我們假設(shè)有兩個(gè)id相同Student,如果他們id相同,在轉(zhuǎn)成Map的時(shí)候,取name大一個(gè),小的將會(huì)被丟棄。

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

    
    //可能上面比較復(fù)雜,這編寫一個(gè)命令式
    //Map<String,Student>
    Stream.of(studentA, studentB, studentC)
        .collect(Collectors
            .toMap(Student::getId,
                Function.identity(),
                (s1, s2) -> {
              
                  //這里使用compareTo 方法 s1>s2 會(huì)返回1,s1==s2 返回0 ,否則返回-1
                  if (((Student) s1).name.compareTo(((Student) s2).name) < -1) {
                    return s2;
                  } else {
                    return s1;
                  }
                }));

如果不想使用默認(rèn)的HashMap 或者 ConcurrentHashMap , 第三個(gè)重載方法還可以使用自定義的Map對(duì)象(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(),這兩者區(qū)別也僅是單線程和多線程的使用場(chǎng)景。為什么要groupingBy歸類為前后處理呢?groupingBy 是在數(shù)據(jù)收集前分組的,再將分好組的數(shù)據(jù)傳遞給下游的收集器。

這是 groupingBy最長(zhǎng)的參數(shù)的函數(shù)classifier 是分類器,mapFactory map的工廠,downstream下游的收集器,正是downstream 的存在,可以在數(shù)據(jù)傳遞個(gè)下游之前做很多的騷操作。

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) 

示例:這里將一組數(shù)整型數(shù)分為正數(shù)、負(fù)數(shù)、零,groupingByConcurrent()的參數(shù)也是跟它一樣的就不舉例了。

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

字面意思話就叫分區(qū)好了,但是partitioningBy最多只能將數(shù)據(jù)分為兩部分,因?yàn)閜artitioningBy分區(qū)的依據(jù)Predicate,而Predicate只會(huì)有true 和false 兩種結(jié)果,所有partitioningBy最多只能將數(shù)據(jù)分為兩組。partitioningBy除了分類器與groupingBy 不一樣外,其他的參數(shù)都相同。

示例:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論