日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java8 collect 类型转换_Java 8 新特性 Stream类的collect方法

發(fā)布時間:2024/9/19 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java8 collect 类型转换_Java 8 新特性 Stream类的collect方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.Collectors.toList():轉(zhuǎn)換成List集合。/?Collectors.toSet():轉(zhuǎn)換成set集合。

System.out.println(Stream.of("a", "b", "c","a").collect(Collectors.toSet()));

2.Collectors.toCollection(TreeSet::new):轉(zhuǎn)換成特定的set集合。

TreeSet treeSet = Stream.of("a", "c", "b", "a").collect(Collectors.toCollection(TreeSet::new));

System.out.println(treeSet);

3.Collectors.toMap(keyMapper, valueMapper, mergeFunction):轉(zhuǎn)換成map。

Map collect = Stream.of("a", "b", "c", "a").collect(Collectors.toMap(x -> x, x -> x + x,(oldVal, newVal) -> newVal)));

collect.forEach((k,v) -> System.out.println(k + ":" + v));

補充

關于合并函數(shù)?BinaryOperator mergeFunction對象

當toMap中沒有用合并函數(shù)時,出現(xiàn)key重復時,會拋出異常 :??Exception in thread "main" java.lang.IllegalStateException: Duplicate key aa

當使用合并函數(shù)時,可通過Labmda表達式,對重復值進行處理

4.Collectors.minBy(Integer::compare):求最小值,相對應的當然也有maxBy方法。

5.Collectors.averagingInt(x->x):求平均值,同時也有averagingDouble、averagingLong方法。

6.Collectors.summingInt(x -> x)):求和。

7.Collectors.summarizingDouble(x -> x):可以獲取最大值、最小值、平均值、總和值、總數(shù)。

DoubleSummaryStatistics summaryStatistics = Stream.of(1, 3, 4).collect(Collectors.summarizingDouble(x -> x));

System.out.println(summaryStatistics .getAverage());

8.?Collectors.groupingBy(x -> x):有三種方法,查看源碼可以知道前兩個方法最終調(diào)用第三個方法,

第二個參數(shù)默認HashMap::new??第三個參數(shù)默認Collectors.toList()

Map> map = Stream.of(1, 3, 3, 2).collect(Collectors.groupingBy(Function.identity()));

System.out.println(map);

Map map1 = Stream.of(1, 3, 3, 2).collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(x -> x)));

System.out.println(map1);

HashMap> hashMap = Stream.of(1, 3, 3, 2).collect(Collectors.groupingBy(Function.identity(), HashMap::new, Collectors.mapping(x -> x + 1, Collectors.toList())));

System.out.println(hashMap);

補充: identity()是Function類的靜態(tài)方法,和 x->x 是一個意思,

當僅僅需要自己返回自己時,使用identity()能更清楚的表達作者的意思.

寫的復雜一點,繞一點,對理解很有好處.下邊是運行結(jié)果:

9.Collectors.partitioningBy(x -> x > 2),把數(shù)據(jù)分成兩部分,key為ture/false。第一個方法也是調(diào)用第二個方法,第二個參數(shù)默認為Collectors.toList()

Map> map = Stream.of(1, 3, 3, 2).collect(Collectors.partitioningBy(x -> x > 2));

Map longMap = Stream.of(1, 3, 3, 2).collect(Collectors.partitioningBy(x -> x > 1, Collectors.counting()));

10.Collectors.joining(","):拼接字符串。

System.out.println(Stream.of("1", "3", "3", "2").collect(Collectors.joining(",")));

11.Collectors.collectingAndThen(Collectors.toList(), x -> x.size()):先執(zhí)行collect操作后再執(zhí)行第二個參數(shù)的表達式。這里是先塞到集合,再得出集合長度。

Integer integer = Stream.of("1", "2", "3").collect(Collectors.collectingAndThen(Collectors.toList(), x -> x.size()));

12.Collectors.mapping(...):跟Stream的map操作類似,只是參數(shù)有點區(qū)別

System.out.println(Stream.of(1, 3, 5).collect(Collectors.mapping(x -> x + 1, Collectors.toList())));

標簽:Java,Stream,Collectors,System,collect,println,out

來源: https://www.cnblogs.com/Deters/p/11137532.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結(jié)

以上是生活随笔為你收集整理的java8 collect 类型转换_Java 8 新特性 Stream类的collect方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。