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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
List對象去重和按照某個(gè)字段排序的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狶ist對象去重和按照某個(gè)字段排序的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
spring boot idea maven依賴找不到問題處理方法
這篇文章主要介紹了spring boot idea 偶爾maven依賴找不到問題,這里總結(jié)了幾種處理方法,方便嘗試排查,對spring boot idea maven依賴找不到問題感興趣的朋友跟隨小編一起看看吧2023-08-08
懶人 IDEA 插件推薦: EasyCode 一鍵幫你生成所需代碼(Easycode用法)
這篇文章主要介紹了懶人 IDEA 插件推薦: EasyCode 一鍵幫你生成所需代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
詳解IDEA啟動(dòng)多個(gè)微服務(wù)的配置方法
這篇文章主要介紹了詳解IDEA啟動(dòng)多個(gè)微服務(wù)的配置方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
SpringBoot開發(fā)案例 分布式集群共享Session詳解
這篇文章主要介紹了SpringBoot開發(fā)案例 分布式集群共享Session詳解,在分布式系統(tǒng)中,為了提升系統(tǒng)性能,通常會(huì)對單體項(xiàng)目進(jìn)行拆分,分解成多個(gè)基于功能的微服務(wù),可能還會(huì)對單個(gè)微服務(wù)進(jìn)行水平擴(kuò)展,保證服務(wù)高可用,需要的朋友可以參考下2019-07-07

