最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

Collectors.toMap:

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


    //Function.identity() 獲取這個對象本身,那么結(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重復的該怎么處理?這里我們假設(shè)有兩個id相同Student,如果他們id相同,在轉(zhuǎn)成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(),這兩者區(qū)別也僅是單線程和多線程的使用場景。為什么要groupingBy歸類為前后處理呢?groupingBy 是在數(shù)據(jù)收集前分組的,再將分好組的數(shù)據(jù)傳遞給下游的收集器。

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

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ù)、負數(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ù)分為兩部分,因為partitioningBy分區(qū)的依據(jù)Predicate,而Predicate只會有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()));

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JavaStream將List轉(zhuǎn)為Map示例

    JavaStream將List轉(zhuǎn)為Map示例

    這篇文章主要為大家介紹了JavaStream將List轉(zhuǎn)為Map示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • 基于mybatis中test條件中單引號雙引號的問題

    基于mybatis中test條件中單引號雙引號的問題

    這篇文章主要介紹了基于mybatis中test條件中單引號雙引號的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring?Cloud?OpenFeign實例介紹使用方法

    Spring?Cloud?OpenFeign實例介紹使用方法

    Spring?Cloud?OpenFeign?對?Feign?進行了二次封裝,使得在?Spring?Cloud?中使用?Feign?的時候,可以做到使用?HTTP?請求訪問遠程服務(wù),就像調(diào)用本地方法一樣的,開發(fā)者完全感知不到這是在調(diào)用遠程訪問,更感知不到在訪問?HTTP?請求
    2022-09-09
  • Java?在游戲中探索數(shù)組二維數(shù)組

    Java?在游戲中探索數(shù)組二維數(shù)組

    數(shù)組和二維數(shù)組感覺用王者榮耀的裝備欄來舉例解釋,應(yīng)該更易懂一些。從基礎(chǔ)開始講,后續(xù)會講到JAVA高級,中間會穿插面試題和項目實戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03
  • Spring JPA 增加字段執(zhí)行異常問題及解決

    Spring JPA 增加字段執(zhí)行異常問題及解決

    這篇文章主要介紹了Spring JPA 增加字段執(zhí)行異常問題及解決,具有很好的參考價值,
    2022-06-06
  • java分布式流式處理組件Producer分區(qū)理論

    java分布式流式處理組件Producer分區(qū)理論

    這篇文章主要為大家介紹了java分布式流式處理組件Producer分區(qū)理論詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • java實現(xiàn)斗地主發(fā)牌系統(tǒng)

    java實現(xiàn)斗地主發(fā)牌系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)斗地主發(fā)牌系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Java異或技操作給任意的文件加密原理及使用詳解

    Java異或技操作給任意的文件加密原理及使用詳解

    這篇文章主要介紹了Java異或技操作給任意的文件加密原理及使用詳解,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Jackson使用示例-Bean、XML、Json之間相互轉(zhuǎn)換

    Jackson使用示例-Bean、XML、Json之間相互轉(zhuǎn)換

    Jackson是一個強大工具,可用于Json、XML、實體之間的相互轉(zhuǎn)換,JacksonXmlElementWrapper用于指定List等集合類,外圍標簽名,JacksonXmlProperty指定包裝標簽名,或者指定標簽內(nèi)部屬性名,JacksonXmlRootElement指定生成xml根標簽的名字,JacksonXmlText指定當前這個值
    2024-05-05
  • 解析Java的可變長參數(shù)列表及其使用時的注意點

    解析Java的可變長參數(shù)列表及其使用時的注意點

    這篇文章主要介紹了解析Java的可變參數(shù)列表及其使用時的注意點,注意可變參數(shù)必須位于最后一項,需要的朋友可以參考下
    2016-03-03

最新評論

芦溪县| 凌源市| 万年县| 甘德县| 宜州市| 营山县| 涿州市| 上犹县| 和平县| 饶阳县| 融水| 项城市| 内丘县| 迁西县| 阳曲县| 定州市| 金昌市| 彭山县| 万载县| 鹤岗市| 正定县| 什邡市| 江门市| 遂溪县| 大悟县| 萨嘎县| 巴林左旗| 宁蒗| 宜兰县| 甘德县| 四子王旗| 清远市| 科技| 广丰县| 丰顺县| 岳西县| 浦北县| 绥棱县| 乌兰察布市| 湖南省| 城固县|