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

Java8中Stream流求最大值最小值的實現(xiàn)示例

 更新時間:2023年04月25日 11:06:14   作者:Archie_java  
本文主要介紹了Java8中Stream流求最大值最小值的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、BigDecimal 求最大值和最小值

1. stream().reduce()實現(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()實現(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()實現(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()實現(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()實現(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()實現(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()實現(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()實現(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()實現(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()實現(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()實現(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流求最大值最小值的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java8 Stream流求最大值最小值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatisPlus?TypeHandler自定義字段類型轉(zhuǎn)換Handler

    MyBatisPlus?TypeHandler自定義字段類型轉(zhuǎn)換Handler

    這篇文章主要為大家介紹了MyBatisPlus?TypeHandler自定義字段類型轉(zhuǎn)換Handler示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Spring?Security權(quán)限管理實現(xiàn)接口動態(tài)權(quán)限控制

    Spring?Security權(quán)限管理實現(xiàn)接口動態(tài)權(quán)限控制

    這篇文章主要為大家介紹了Spring?Security權(quán)限管理實現(xiàn)接口動態(tài)權(quán)限控制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • BeanUtils.copyProperties復(fù)制對象結(jié)果為空的原因分析

    BeanUtils.copyProperties復(fù)制對象結(jié)果為空的原因分析

    這篇文章主要介紹了BeanUtils.copyProperties復(fù)制對象結(jié)果為空的原因分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • IDEA新手必備之各種快捷鍵詳解

    IDEA新手必備之各種快捷鍵詳解

    這篇文章主要介紹了IDEA新手必備之各種快捷鍵詳解,文中有非常詳細(xì)的快捷鍵介紹,對正在使用IDEA的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java Optional<Foo>轉(zhuǎn)換成List<Bar>的實例方法

    Java Optional<Foo>轉(zhuǎn)換成List<Bar>的實例方法

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于Java Optional<Foo>轉(zhuǎn)換成List<Bar>的實例方法,有需要的朋友們可以跟著學(xué)習(xí)下。
    2021-06-06
  • jdbcTemplate使用方法實例解析

    jdbcTemplate使用方法實例解析

    這篇文章主要介紹了jdbcTemplate使用方法實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • BeanDefinition基礎(chǔ)信息講解

    BeanDefinition基礎(chǔ)信息講解

    今天小編就為大家分享一篇關(guān)于BeanDefinition基礎(chǔ)信息講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Java設(shè)計模式之里氏替換原則精解

    Java設(shè)計模式之里氏替換原則精解

    設(shè)計模式(Design pattern)代表了最佳的實踐,通常被有經(jīng)驗的面向?qū)ο蟮能浖_發(fā)人員所采用。設(shè)計模式是軟件開發(fā)人員在軟件開發(fā)過程中面臨的一般問題的解決方案。本篇介紹設(shè)計模式七大原則之一的里氏替換原則
    2022-02-02
  • 基于Java判斷網(wǎng)絡(luò)是否正常代碼實例

    基于Java判斷網(wǎng)絡(luò)是否正常代碼實例

    這篇文章主要介紹了基于Java判斷網(wǎng)絡(luò)是否正常代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • JDK動態(tài)代理詳細(xì)解析

    JDK動態(tài)代理詳細(xì)解析

    這篇文章主要介紹了JDK動態(tài)代理詳細(xì)解析,在Java的動態(tài)代理機(jī)制中,有兩個重要的類和接口,一個是InvoInvocationHandler(接口)、Proxy(類),這一個類和接口是我們動態(tài)代理所必須用到的,需要的朋友可以參考下
    2023-11-11

最新評論