迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 中的 Stream 的 reduce 操作

作者:迹忆客 最近更新:2023/11/13 浏览次数:

本文将讨论 reduce() 操作细节并讨论它的一些示例。在讨论 reduce() 操作之前。让我们首先讨论减少。


Java 中的 reduce 是什么

JDK 中的许多终端操作(例如平均值、求和、最小值、最大值和计数)结合流的内容以输出单个值。还原操作执行相同的操作并返回单个流。

此外,JDK 包括返回集合而不是单个值的还原操作。许多还原过程完成特定的工作,例如计算值的平均值或对项目进行分类。

另一方面,JDK 具有称为还原和收集的通用还原操作。


Java 8 中的流 reduce() 操作

reduce() 操作是一种通用的还原操作。reduce() 操作的语法是:

T reduce(T identity, BinaryOperator<T> accumulator)

reduce 操作有两个参数:

  • identityidentity 元素既是还原的起始值,也是流不包含元素时的默认结果。
  • accumulatoraccumulator 函数接受两个参数:部分还原结果和流的下一个元素。它给了我们一个新的部分结果。

reduce() 操作是一种终端操作。


在 Java 中使用 reduce() 运算求和

让我们使用 Java 中的 Stream API 计算数组中所有元素的总和。看下面的代码:

import java.util.Arrays;
public class SimpleTesting {
  public static void main(String args[]) {
    int[] numbers = {45, 56, 87, 323, 47, 20, 658, 985, 78, 123};
    int sum = Arrays.stream(numbers).reduce(0, (a, b) -> a + b);

    System.out.print("sum: " + sum);
  }
}

输出:

sum: 2422

如果我们不使用 reduce() 函数,我们将不得不编写下面的代码来获取所有元素的总和。请参见下面的示例。

import java.util.Arrays;
public class SimpleTesting {
  public static void main(String args[]) {
    int[] numbers = {45, 56, 87, 323, 47, 20, 658, 985, 78, 123};
    int sum = 0;
    for (int i : numbers) {
      sum += i;
    }
    System.out.println("sum : " + sum);
  }
}

输出:

sum : 2422

在 Java 中使用 reduce() 和方法参考求和

我们还可以按如下方式传递 sum() 方法引用 Integer::sum 以获得所有元素的总和。看下面的代码:

import java.util.Arrays;
public class SimpleTesting {
  public static void main(String args[]) {
    int[] numbers = {45, 56, 87, 323, 47, 20, 658, 985, 78, 123};
    int sum = Arrays.stream(numbers).reduce(0, Integer::sum);

    System.out.print("sum: " + sum);
  }
}

输出:

sum: 2422

如果数组为空并且我们通过它来获取元素的总和,那么它不会抛出任何异常,而是给出零结果。请参见下面的示例。

import java.util.Arrays;
public class SimpleTesting {
  public static void main(String args[]) {
    int[] numbers = {};
    int sum = Arrays.stream(numbers).reduce(0, Integer::sum);

    System.out.print("sum: " + sum);
  }
}

输出:

sum: 0

如我们所见,如果数组为空,则返回标识元素。


在 Java 中使用 reduce() 操作查找最小值和最大值

通常,要找到数组中的最小和最大元素,我们使用 for 循环和 if-else 语句的组合,例如:

public class SimpleTesting {
  public static void main(String args[]) {
    int[] numbers = {45, 56, 87, 323, 47, 20, 658, 985, 78, 123};
    // finding the min element
    int min = numbers[0];
    for (int i : numbers) {
      if (min > i) {
        min = i;
      }
    }
    // finding the max element
    int max = numbers[0];
    for (int i : numbers) {
      if (max < i) {
        max = i;
      }
    }
    // printing the max and min value
    System.out.println("max: " + max);
    System.out.println("min: " + min);
  }
}

输出:

max: 985
min: 20

我们可以使用 reduce() 操作在数组中找到最小和最大元素,这使我们的代码更加简洁。请参见下面的示例。

import java.util.Arrays;

public class SimpleTesting {
  public static void main(String args[]) {
    int[] numbers = {45, 56, 87, 323, 47, 20, 658, 985, 78, 123};
    // finding the min element
    int min = Arrays.stream(numbers).reduce(Integer.MAX_VALUE, (a, b) -> a < b ? a : b);
    // finding the max element
    int max = Arrays.stream(numbers).reduce(Integer.MIN_VALUE, (a, b) -> a > b ? a : b);
    // printing the max and min value
    System.out.println("max: " + max);
    System.out.println("min: " + min);
  }
}

输出:

max: 985
min: 20

我们还可以使用方法引用来计算数组中的最小和最大元素。看下面的代码:

import java.util.Arrays;
public class SimpleTesting {
  public static void main(String args[]) {
    int[] numbers = {45, 56, 87, 323, 47, 20, 658, 985, 78, 123};
    // finding the min element
    int min = Arrays.stream(numbers).reduce(Integer.MAX_VALUE, Integer::min);
    // finding the max element
    int max = Arrays.stream(numbers).reduce(Integer.MIN_VALUE, Integer::max);
    // printing the max and min value
    System.out.println("max: " + max);
    System.out.println("min: " + min);
  }
}

输出:

max: 985
min: 20

结论

在本教程中,我们了解了还原操作。然后,我们深入探讨了 reduce() 操作的主题。最后,我们看到了几个如何简洁地执行基本算术函数的示例。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

在 Pandas 的列中展平层次索引

发布时间:2024/04/24 浏览次数:1782 分类:Python

在这篇文章中,我们将使用不同的函数来使用 Pandas DataFrame 列来展平层次索引。我们将使用的方法是重置索引和 as_index() 函数。

Pandas 中的 Groupby 索引列

发布时间:2024/04/23 浏览次数:89 分类:Python

本教程将介绍如何使用 Python Pandas Groupby 对数据进行分类,然后将函数应用于类别。通过示例使用 groupby() 函数按 Pandas 中的多个索引列进行分组。

Pandas 中的散点矩阵

发布时间:2024/04/23 浏览次数:125 分类:Python

本教程演示了如何使用 scatter_matrix 函数在 Pandas 中创建散点图。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便