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

淡淡的烟草味﹌ 2022-09-14 09:28 378阅读 0赞

1 BigDecimal类型数值求最大最小值

1.1 for循环实现

  1. List<BigDecimal> list = new ArrayList<>(Arrays.asList(new BigDecimal(1), new BigDecimal(2)));
  2. BigDecimal max = list.get(0);
  3. BigDecimal min = list.get(0);
  4. for (BigDecimal decimal : list) {
  5. if (max.compareTo(decimal) < 0) {
  6. max = decimal;
  7. }
  8. if (min.compareTo(decimal) > 0) {
  9. min = decimal;
  10. }
  11. }

1.2 stream().reduce()实现

  1. List<BigDecimal> list = new ArrayList<>(Arrays.asList(new BigDecimal(1), new BigDecimal(2)));
  2. BigDecimal max = list.stream().reduce(list.get(0), BigDecimal::max);
  3. BigDecimal min = list.stream().reduce(list.get(0), BigDecimal::min);

1.3 stream().max()或stream().min()实现

  1. List<BigDecimal> list = new ArrayList<>(Arrays.asList(new BigDecimal(1), new BigDecimal(2)));
  2. BigDecimal max = list.stream().max(Comparator.comparing(x -> x)).orElse(null);
  3. BigDecimal min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);

1.4 Collectors.maxBy()或Collectors.minBy()实现

  1. List<BigDecimal> list = new ArrayList<>(Arrays.asList(new BigDecimal(1), new BigDecimal(2)));
  2. BigDecimal max = list.stream().collect(Collectors.maxBy(Comparator.comparing(x -> x))).orElse(null);
  3. BigDecimal min = list.stream().collect(Collectors.minBy(Comparator.comparing(x -> x))).orElse(null);

2 Integer类型数值求最大最小值

​ 2.1 for循环实现

  1. List<Integer> list = new ArrayList<>(Arrays.asList(1, 2));
  2. Integer max = list.get(0);
  3. Integer min = list.get(0);
  4. for (Integer num : list) {
  5. if (num > max) {
  6. max = num;
  7. }
  8. if (num < min) {
  9. min = num;
  10. }
  11. }

2.2 stream().reduce()实现

  1. List<Integer> list = new ArrayList<>(Arrays.asList(1, 2));
  2. Integer max = list.stream().reduce(list.get(0), Integer::max);
  3. Integer min = list.stream().reduce(list.get(0), Integer::min);

2.3 Collectors.summarizingInt()实现

  1. List<Integer> list = new ArrayList<>(Arrays.asList(1, 2));
  2. IntSummaryStatistics intSummaryStatistics = list.stream().collect(Collectors.summarizingInt(x -> x));
  3. Integer max = intSummaryStatistics.getMax();
  4. Integer min = intSummaryStatistics.getMin();

2.4 stream().max()或stream().min()实现

  1. List<Integer> list = new ArrayList<>(Arrays.asList(1, 2));
  2. Integer max = list.stream().max(Comparator.comparing(x -> x)).orElse(null);
  3. Integer min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);

2.5 Collectors.maxBy()或Collectors.minBy()实现

  1. List<Integer> list = new ArrayList<>(Arrays.asList(1, 2));
  2. Integer max = list.stream().collect(Collectors.maxBy(Comparator.comparing(x -> x))).orElse(null);
  3. Integer min = list.stream().collect(Collectors.minBy(Comparator.comparing(x -> x))).orElse(null);

3 Long类型数值求最大最小值

3.1 for循环实现

  1. List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L));
  2. Long max = list.get(0);
  3. Long min = list.get(0);
  4. for (Long num : list) {
  5. if (num > max) {
  6. max = num;
  7. }
  8. if (num < min) {
  9. min = num;
  10. }
  11. }

3.2 stream().reduce()实现

  1. List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L));
  2. Long max = list.stream().reduce(list.get(0), Long::max);
  3. Long min = list.stream().reduce(list.get(0), Long::min);

3.3 Collectors.summarizingLong()实现

  1. List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L));
  2. LongSummaryStatistics summaryStatistics = list.stream().collect(Collectors.summarizingLong(x -> x));
  3. Long max = summaryStatistics.getMax();
  4. Long min = summaryStatistics.getMin();

3.4 stream().max()或stream().min()实现

  1. List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L));
  2. Long max = list.stream().max(Comparator.comparing(x -> x)).orElse(null);
  3. Long min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);

3.5 Collectors.maxBy()或Collectors.minBy()实现

  1. List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L));
  2. Long max = list.stream().collect(Collectors.maxBy(Comparator.comparing(x -> x))).orElse(null);
  3. Long min = list.stream().collect(Collectors.minBy(Comparator.comparing(x -> x))).orElse(null);

4 Double类型数值求最大最小值

4.1 for循环实现

  1. List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d));
  2. Double max = list.get(0);
  3. Double min = list.get(0);
  4. for (Double num : list) {
  5. if (num > max) {
  6. max = num;
  7. }
  8. if (num < min) {
  9. min = num;
  10. }
  11. }

4.2 stream().reduce()实现

  1. List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d));
  2. Double max = list.stream().reduce(list.get(0), Double::max);
  3. Double min = list.stream().reduce(list.get(0), Double::min);

4.3 Collectors.summarizingDouble()实现

  1. List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d));
  2. DoubleSummaryStatistics summaryStatistics = list.stream().collect(Collectors.summarizingDouble(x -> x));
  3. Double max = summaryStatistics.getMax();
  4. Double min = summaryStatistics.getMin();

4.4 stream().max()或stream().min()实现

  1. List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d));
  2. Double max = list.stream().max(Comparator.comparing(x -> x)).orElse(null);
  3. Double min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);

4.5 Collectors.maxBy()或Collectors.minBy()实现

  1. List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d));
  2. Double max = list.stream().collect(Collectors.maxBy(Comparator.comparing(x -> x))).orElse(null);
  3. Double min = list.stream().collect(Collectors.minBy(Comparator.comparing(x -> x))).orElse(null);

发表评论

表情:
评论列表 (有 0 条评论,378人围观)

还没有评论,来说两句吧...

相关阅读

    相关 分治法

    分治法是一种递归的问题解决方法,它将一个大问题划分为多个小问题,然后逐个解决这些小问题,最后将结果合并得到最终的解决方案。对于求最大最小值的问题,可以使用分治法来解决。 以下