java中各種類型用Stream流求最大值最小值方式
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)文章
springboot 自定義配置Boolean屬性不生效的解決
這篇文章主要介紹了springboot 自定義配置Boolean屬性不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
springbootAOP定義切點(diǎn)獲取/修改請(qǐng)求參數(shù)方式
這篇文章主要介紹了springbootAOP定義切點(diǎn)獲取/修改請(qǐng)求參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot實(shí)現(xiàn)API接口多版本支持的示例代碼
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)API接口多版本支持的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
解決grails服務(wù)端口沖突的辦法(grails修改端口號(hào))
grails中默認(rèn)的服務(wù)端口為8080,當(dāng)本機(jī)中需要同時(shí)啟動(dòng)兩個(gè)不同的項(xiàng)目時(shí),就會(huì)造成端口沖突,下面給出解決方法2013-12-12
logback-spring.xml的配置及示例詳解(直接復(fù)制粘貼可用)
在使用logback作為日志框架時(shí),可以創(chuàng)建一個(gè)名為logback-spring.xml的配置文件來(lái)自定義日志輸出的格式和方式,下面這篇文章主要給大家介紹了關(guān)于logback-spring.xml的配置及示例詳解的相關(guān)資料,文中的代碼直接復(fù)制粘貼可用,需要的朋友可以參考下2024-01-01
SpringBoot中的@Configuration注解詳解
這篇文章主要介紹了SpringBoot中的@Configuration注解詳解,Spring Boot推薦使用JAVA配置來(lái)完全代替XML 配置,JAVA配置就是通過(guò) @Configuration和 @Bean兩個(gè)注解實(shí)現(xiàn)的,需要的朋友可以參考下2023-08-08
Java 中synchronize函數(shù)的實(shí)例詳解
這篇文章主要介紹了Java 中synchronize函數(shù)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家理解使用synchronize函數(shù)的使用方法,需要的朋友可以參考下2017-09-09

