栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java BigDecimal、Integer、Long、Double类型数值求最大最小值

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java BigDecimal、Integer、Long、Double类型数值求最大最小值

1 BigDecimal类型数值求最大最小值 1.1 for循环实现
        List list = new ArrayList<>(Arrays.asList(new BigDecimal(1), new BigDecimal(2)));
        BigDecimal max = list.get(0);
        BigDecimal min = list.get(0);
        for (BigDecimal decimal : list) {
            if (max.compareTo(decimal) < 0) {
                max = decimal;
            }
            if (min.compareTo(decimal) > 0) {
                min = decimal;
            }
        }
1.2 stream().reduce()实现
        List 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);
1.3 stream().max()或stream().min()实现 
        List 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);
 1.4 Collectors.maxBy()或Collectors.minBy()实现 
        List list = new ArrayList<>(Arrays.asList(new BigDecimal(1), new BigDecimal(2)));
        BigDecimal max = list.stream().collect(Collectors.maxBy(Comparator.comparing(x -> x))).orElse(null);
        BigDecimal min = list.stream().collect(Collectors.minBy(Comparator.comparing(x -> x))).orElse(null);
2 Integer类型数值求最大最小值 ​ 2.1 for循环实现
        List list = new ArrayList<>(Arrays.asList(1, 2));
        Integer max = list.get(0);
        Integer min = list.get(0);
        for (Integer num : list) {
            if (num > max) {
                max = num;
            }
            if (num < min) {
                min = num;
            }
        }
2.2 stream().reduce()实现
        List 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.3 Collectors.summarizingInt()实现
        List list = new ArrayList<>(Arrays.asList(1, 2));
        IntSummaryStatistics intSummaryStatistics = list.stream().collect(Collectors.summarizingInt(x -> x));
        Integer max = intSummaryStatistics.getMax();
        Integer min = intSummaryStatistics.getMin();
2.4 stream().max()或stream().min()实现
        List 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);
2.5 Collectors.maxBy()或Collectors.minBy()实现
        List list = new ArrayList<>(Arrays.asList(1, 2));
        Integer max = list.stream().collect(Collectors.maxBy(Comparator.comparing(x -> x))).orElse(null);
        Integer min = list.stream().collect(Collectors.minBy(Comparator.comparing(x -> x))).orElse(null);
3 Long类型数值求最大最小值 3.1 for循环实现
        List list = new ArrayList<>(Arrays.asList(1L, 2L));
        Long max = list.get(0);
        Long min = list.get(0);
        for (Long num : list) {
            if (num > max) {
                max = num;
            }
            if (num < min) {
                min = num;
            }
        }
3.2 stream().reduce()实现 
        List 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);
3.3 Collectors.summarizingLong()实现
        List 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.4 stream().max()或stream().min()实现
        List 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);
3.5 Collectors.maxBy()或Collectors.minBy()实现
        List list = new ArrayList<>(Arrays.asList(1L, 2L));
        Long max = list.stream().collect(Collectors.maxBy(Comparator.comparing(x -> x))).orElse(null);
        Long min = list.stream().collect(Collectors.minBy(Comparator.comparing(x -> x))).orElse(null);
4 Double类型数值求最大最小值 4.1 for循环实现
        List list = new ArrayList<>(Arrays.asList(1d, 2d));
        Double max = list.get(0);
        Double min = list.get(0);
        for (Double num : list) {
            if (num > max) {
                max = num;
            }
            if (num < min) {
                min = num;
            }
        }
4.2 stream().reduce()实现 
        List 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);
4.3 Collectors.summarizingDouble()实现
        List list = new ArrayList<>(Arrays.asList(1d, 2d));
        DoubleSummaryStatistics summaryStatistics = list.stream().collect(Collectors.summarizingDouble(x -> x));
        Double max = summaryStatistics.getMax();
        Double min = summaryStatistics.getMin();
4.4 stream().max()或stream().min()实现 
        List 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);
4.5 Collectors.maxBy()或Collectors.minBy()实现
        List list = new ArrayList<>(Arrays.asList(1d, 2d));
        Double max = list.stream().collect(Collectors.maxBy(Comparator.comparing(x -> x))).orElse(null);
        Double min = list.stream().collect(Collectors.minBy(Comparator.comparing(x -> x))).orElse(null);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/315015.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号