Java8中Stream流求最大值最小值的實(shí)現(xiàn)示例
一、BigDecimal 求最大值和最小值
1. stream().reduce()實(shí)現(xiàn)
List<BigDecimal> list = new ArrayList<>(Arrays.asList(new BigDecimal("1"), new BigDecimal("2")));
BigDecimal max = list.stream().reduce(list.get(0), BigDecimal::max);
BigDecimal min = list.stream().reduce(list.get(0), BigDecimal::min);
2. stream().max()或stream().min()實(shí)現(xiàn)
List<BigDecimal> list = new ArrayList<>(Arrays.asList(new BigDecimal("1"), new BigDecimal("2")));
BigDecimal max = list.stream().max(Comparator.comparing(x -> x)).orElse(null);
BigDecimal min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);
二、Integer 求最大值和最小值
1. stream().reduce()實(shí)現(xiàn)
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2)); Integer max = list.stream().reduce(list.get(0), Integer::max); Integer min = list.stream().reduce(list.get(0), Integer::min);
2. Collectors.summarizingInt()實(shí)現(xiàn)
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2)); IntSummaryStatistics intSummaryStatistics = list.stream().collect(Collectors.summarizingInt(x -> x)); Integer max = intSummaryStatistics.getMax(); Integer min = intSummaryStatistics.getMin();
3. stream().max()或stream().min()實(shí)現(xiàn)
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2)); Integer max = list.stream().max(Comparator.comparing(x -> x)).orElse(null); Integer min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);
三、Long 求最大值和最小值
1. stream().reduce()實(shí)現(xiàn)
List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L)); Long max = list.stream().reduce(list.get(0), Long::max); Long min = list.stream().reduce(list.get(0), Long::min);
2. Collectors.summarizingLong()實(shí)現(xiàn)
List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L)); LongSummaryStatistics summaryStatistics = list.stream().collect(Collectors.summarizingLong(x -> x)); Long max = summaryStatistics.getMax(); Long min = summaryStatistics.getMin();
3. stream().max()或stream().min()實(shí)現(xiàn)
List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L)); Long max = list.stream().max(Comparator.comparing(x -> x)).orElse(null); Long min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);
四、Double 求最大值和最小值
1. stream().reduce()實(shí)現(xiàn)
List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d)); Double max = list.stream().reduce(list.get(0), Double::max); Double min = list.stream().reduce(list.get(0), Double::min);
2. Collectors.summarizingLong()實(shí)現(xiàn)
List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d)); DoubleSummaryStatistics summaryStatistics = list.stream().collect(Collectors.summarizingDouble(x -> x)); Double max = summaryStatistics.getMax(); Double min = summaryStatistics.getMin();
3. stream().max()或stream().min()實(shí)現(xiàn)
List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d)); Double max = list.stream().max(Comparator.comparing(x -> x)).orElse(null); Double min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);
到此這篇關(guān)于Java8中Stream流求最大值最小值的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java8 Stream流求最大值最小值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
List對(duì)象去重和按照某個(gè)字段排序的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇List對(duì)象去重和按照某個(gè)字段排序的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
spring boot idea maven依賴(lài)找不到問(wèn)題處理方法
這篇文章主要介紹了spring boot idea 偶爾maven依賴(lài)找不到問(wèn)題,這里總結(jié)了幾種處理方法,方便嘗試排查,對(duì)spring boot idea maven依賴(lài)找不到問(wèn)題感興趣的朋友跟隨小編一起看看吧2023-08-08
懶人 IDEA 插件推薦: EasyCode 一鍵幫你生成所需代碼(Easycode用法)
這篇文章主要介紹了懶人 IDEA 插件推薦: EasyCode 一鍵幫你生成所需代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
詳解IDEA啟動(dòng)多個(gè)微服務(wù)的配置方法
這篇文章主要介紹了詳解IDEA啟動(dòng)多個(gè)微服務(wù)的配置方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
SpringBoot開(kāi)發(fā)案例 分布式集群共享Session詳解
這篇文章主要介紹了SpringBoot開(kāi)發(fā)案例 分布式集群共享Session詳解,在分布式系統(tǒng)中,為了提升系統(tǒng)性能,通常會(huì)對(duì)單體項(xiàng)目進(jìn)行拆分,分解成多個(gè)基于功能的微服務(wù),可能還會(huì)對(duì)單個(gè)微服務(wù)進(jìn)行水平擴(kuò)展,保證服務(wù)高可用,需要的朋友可以參考下2019-07-07
java實(shí)現(xiàn)門(mén)禁系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)門(mén)禁系統(tǒng)的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Java文件操作類(lèi) File實(shí)現(xiàn)代碼
這篇文章主要介紹了Java文件操作類(lèi) File實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08
JavaWeb中的常用的請(qǐng)求傳參注解說(shuō)明
這篇文章主要介紹了JavaWeb中的常用的請(qǐng)求傳參注解說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

