如何在 Java 中使用 Stream allMatch() 和 anyMatch() 函数?
Hello,我们都知道流对于当今 Java 编程和编码的日常需求是多么的重要。 它允许我们以声明的方式过滤内存中的数据,它还允许我们转换对象以及创建复杂的 Stream 管道以声明的方式表示我们的业务逻辑。 但是,我们知道流的所有功能吗? 当然不是。 早些时候,我们已经看到了 filter()
、map
、flatMap
和 findFirst()
方法的示例,今天我们将学习两个最重要的流函数 anyMatch()
和 allMatch()
。 那还等什么呢? 开始吧!
流 allMatch:
我们经常遇到需要检查值列表上的条件的问题。 举个例子,假设有一个包含 500 个对象的数组,我们需要检查每个对象是否可以被 3 整除。
现在,正常的方法是在数组中运行一个循环并检查所有对象的条件。 但是等等,为什么有这么多样板代码? 我们可以使用流,这将在一行中完成! 是的,这是真的。
Stream allMatch 函数的语法如下:
allMatch(Predicate predicate)
在这里,谓词是检查每个元素的条件。 我们还可以提供多个这样的谓词来流式传输。 下面是该函数如何工作的工作示例。
如代码所示,我们可以看到它是如何工作的。 我们有一个大小为 5 的数组,里面有不同的数字。 我们使用 Arrays.stream()
函数将其转换为 Stream。
我们使用了 IntPredicate。 同样,我们可以使用不同类型的谓词
Stream allMatch 代码
import java.util.Arrays;
import java.util.function.IntPredicate;
public class StreamExample {
public static void main(String[] args) {
int[] arr = new int[] {3, 18, 15, 30, 60};
IntPredicate isDivisibleByThree = a -> (a%3 == 0);
IntPredicate isDivisibleByFive = a -> (a%5 == 0);
IntPredicate isDivisibleByThreeAndFive = isDivisibleByThree
.and(isDivisibleByFive);
boolean isDivisibleByThreeResult = Arrays.stream(arr)
.allMatch(isDivisibleByThree);
System.out.println("divisible by three: "+isDivisibleByThreeResult);
boolean isDivisibleByFiveResult = Arrays.stream(arr)
.allMatch(isDivisibleByFive);
System.out.println("divisible by five: "+isDivisibleByFiveResult);
boolean isDivisibleByThreeAndFiveResult = Arrays.stream(arr)
.allMatch(isDivisibleByThreeAndFive);
System.out.println("divisible by three and five: "
+isDivisibleByThreeAndFiveResult);
}
}
正如所见,我们有多个谓词,它成功地给了我们结果。 谓词也可以是任何复杂的操作。 我已经演示了使用整数,你们也可以尝试动手操作 char 或对象。 我使用了 IntPredicate。 对于对象,我们也可以使用 Predicate<YourObject>
。
Stream anyMatch:
看完上面的代码,大家应该对 anyMatch
做的流有信心了吧。 anyMatch
基本上在数组/列表或任何集合中搜索任何匹配项。 只要 1 就足以使答案为真。
因此,继续我们之前的代码和示例,让我们了解这是如何发生的。 但在此之前,让我们看看stream anyMatch的语法
stream anyMatch 的语法如下:
anyMatch(Predicate predicate)
在这里,谓词是检查每个元素的条件。 我们还可以提供多个这样的谓词来流式传输。 下面是该函数如何工作的工作示例。 扩展和重用我们之前的示例。
让我们使用相同的数组,看看 anyMatch
函数会发生什么。
Stream anyMatch 代码
import java.util.Arrays;
import java.util.function.IntPredicate;
public class StreamExample {
public static void main(String[] args) {
int[] arr = new int[] {3, 18, 15, 30, 60};
IntPredicate isDivisibleByThree = a -> (a%3 == 0);
IntPredicate isDivisibleByFive = a -> (a%5 == 0);
IntPredicate isDivisibleByThreeAndFive = isDivisibleByThree
.and(isDivisibleByFive);
boolean isDivisibleByThreeResult = Arrays.stream(arr)
.anyMatch(isDivisibleByThree);
System.out.println("divisible by three: "+isDivisibleByThreeResult);
boolean isDivisibleByFiveResult = Arrays.stream(arr)
.anyMatch(isDivisibleByFive);
System.out.println("divisible by five: "+isDivisibleByFiveResult);
boolean isDivisibleByThreeAndFiveResult = Arrays.stream(arr)
.anyMatch(isDivisibleByThreeAndFive);
System.out.println("divisible by three and five: "
+isDivisibleByThreeAndFiveResult);
}
}
那么现在,你们必须知道 Stream API 的 allMatch()
和 anyMatch()
函数是如何工作的。 但是,在使用这些功能之前需要记住几点。
首先,花 2 分钟时间集思广益,想一想如果流是无限的会发生什么。
这些功能将如何处理它们? 还要考虑如果传递对象,如果所需字段不存在或为空或为空怎么办?
花 2 分钟时间思考这些问题,然后我们将逐一讨论。
要记住的要点:
- 如果流为空怎么办? anyMatch - false,allMatch - true。 返回的答案是流是空的。
- 这两个函数都是短路端子操作。 这意味着,如果出现无限流,函数可能会在有限时间内终止。
Stream.allMatch()
为空流返回 true,而 anyMatch
返回 false。 这听起来可能有点令人困惑。 但不用担心,朋友们,我们在这里解决所有的疑问和问题。
allMatch()
返回真。
这被称为空洞的真相。 空集合的所有成员都满足我们的条件; 毕竟,你能指出一个没有的吗?
同样,anyMatch
返回 false,因为我们无法在集合中找到与条件匹配的元素。 这让很多人感到困惑,但事实证明这是为空集定义“任何”和“所有”的最有用和最一致的方式。
希望你们喜欢这篇文章! 让我们在下一篇文章中见面。
相关文章
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
如何在 JavaScript 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。