欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中各種類型用Stream流求最大值最小值方式

 更新時(shí)間:2025年06月23日 08:47:47   作者:yololee_  
這篇文章主要介紹了java中各種類型用Stream流求最大值最小值方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

java中各種類型用Stream流求最大值最小值

一、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);

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring框架IOC容器底層原理詳解

    Spring框架IOC容器底層原理詳解

    在java當(dāng)中一個(gè)類想要使用另一個(gè)類的方法,就必須在這個(gè)類當(dāng)中創(chuàng)建這個(gè)類的對象,Spring將創(chuàng)建對象的權(quán)利給了IOC,在IOC當(dāng)中創(chuàng)建了ABC三個(gè)對象,那么我們我們其他的類只需要調(diào)用集合,大大的解決了程序耦合性的問題
    2022-07-07
  • springboot 自定義配置Boolean屬性不生效的解決

    springboot 自定義配置Boolean屬性不生效的解決

    這篇文章主要介紹了springboot 自定義配置Boolean屬性不生效的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springbootAOP定義切點(diǎn)獲取/修改請求參數(shù)方式

    springbootAOP定義切點(diǎn)獲取/修改請求參數(shù)方式

    這篇文章主要介紹了springbootAOP定義切點(diǎn)獲取/修改請求參數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • SpringBoot實(shí)現(xiàn)API接口多版本支持的示例代碼

    SpringBoot實(shí)現(xiàn)API接口多版本支持的示例代碼

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)API接口多版本支持的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 解決grails服務(wù)端口沖突的辦法(grails修改端口號)

    解決grails服務(wù)端口沖突的辦法(grails修改端口號)

    grails中默認(rèn)的服務(wù)端口為8080,當(dāng)本機(jī)中需要同時(shí)啟動(dòng)兩個(gè)不同的項(xiàng)目時(shí),就會造成端口沖突,下面給出解決方法
    2013-12-12
  • logback-spring.xml的配置及示例詳解(直接復(fù)制粘貼可用)

    logback-spring.xml的配置及示例詳解(直接復(fù)制粘貼可用)

    在使用logback作為日志框架時(shí),可以創(chuàng)建一個(gè)名為logback-spring.xml的配置文件來自定義日志輸出的格式和方式,下面這篇文章主要給大家介紹了關(guān)于logback-spring.xml的配置及示例詳解的相關(guān)資料,文中的代碼直接復(fù)制粘貼可用,需要的朋友可以參考下
    2024-01-01
  • SpringBoot中的@Configuration注解詳解

    SpringBoot中的@Configuration注解詳解

    這篇文章主要介紹了SpringBoot中的@Configuration注解詳解,Spring Boot推薦使用JAVA配置來完全代替XML 配置,JAVA配置就是通過 @Configuration和 @Bean兩個(gè)注解實(shí)現(xiàn)的,需要的朋友可以參考下
    2023-08-08
  • Java 中synchronize函數(shù)的實(shí)例詳解

    Java 中synchronize函數(shù)的實(shí)例詳解

    這篇文章主要介紹了Java 中synchronize函數(shù)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家理解使用synchronize函數(shù)的使用方法,需要的朋友可以參考下
    2017-09-09
  • 淺析Java如何保護(hù)敏感數(shù)據(jù)

    淺析Java如何保護(hù)敏感數(shù)據(jù)

    在當(dāng)今數(shù)字化時(shí)代,數(shù)據(jù)安全成為了軟件開發(fā)中至關(guān)重要的課題,本文將深入探討 Java 安全領(lǐng)域,聚焦于敏感數(shù)據(jù)保護(hù)的策略與實(shí)踐,感興趣的小伙伴可以了解下
    2025-05-05
  • 解決@JsonIgnore的使用以及自己踩坑

    解決@JsonIgnore的使用以及自己踩坑

    這篇文章主要介紹了解決@JsonIgnore的使用以及自己踩坑,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07

最新評論