boolean allMatch(Predicate<? super T> predicate)
Returns whether all elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then true is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
API Note:
This method evaluates the universal quantification of the predicate over the elements of the stream (for all x P(x)). If the stream is empty, the quantification is said to be vacuously satisfied and is always true (regardless of P(x)).
Parameters:
predicate - a non-interfering, stateless predicate to apply to elements of this stream
Returns:
true if either all elements of the stream match the provided predicate or the stream is empty, otherwise false
anyMatch
boolean anyMatch(Predicate<? super T> predicate)
Returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then false is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
API Note:
This method evaluates the existential quantification of the predicate over the elements of the stream (for some x P(x)).
Parameters:
predicate - a non-interfering, stateless predicate to apply to elements of this stream
Returns:
true if any elements of the stream match the provided predicate, otherwise false
noneMatch
boolean noneMatch(Predicate<? super T> predicate)
Returns whether no elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then true is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
API Note:
This method evaluates the universal quantification of the negated predicate over the elements of the stream (for all x ~P(x)). If the stream is empty, the quantification is said to be vacuously satisfied and is always true, regardless of P(x).
Parameters:
predicate - a non-interfering, stateless predicate to apply to elements of this stream
Returns:
true if either no elements of the stream match the provided predicate or the stream is empty, otherwise false
// streams/Matching.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Demonstrates short-circuiting of *Match() operations
import java.util.function.*;
import java.util.stream.*;
interface Matcher extends BiPredicate<Stream<Integer>, Predicate<Integer>> {}
public class Matching {
static void show(Matcher match, int val) {
System.out.println(
match.test(
IntStream.rangeClosed(1, 9).boxed().peek(n -> System.out.format("%d ", n)),
n -> n < val));
}
public static void main(String[] args) {
show(Stream::allMatch, 10);
// show(Stream::allMatch, 6); // 1 2 3 4 5 6 false
// show(Stream::allMatch, 5); // 1 2 3 4 5 false
show(Stream::allMatch, 4);
show(Stream::anyMatch, 2);
show(Stream::anyMatch, 0);
show(Stream::noneMatch, 5);
show(Stream::noneMatch, 0);
}
}
/* Output:
1 2 3 4 5 6 7 8 9 true
1 2 3 4 false
1 true
1 2 3 4 5 6 7 8 9 false
1 false
1 2 3 4 5 6 7 8 9 true
*/
references:
1. On Java 8 - Bruce Eckel
2. https://docs.oracle.com/javase/8/docs/api/java/util/function/BiPredicate.html#test-T-U-
4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/Matching.java
本文探讨了Java Stream API中的三种Match操作:allMatch、anyMatch和noneMatch,这些操作用于确定流中的元素是否满足特定的谓词条件。通过实例演示了这些方法的短路特性,当结果可以确定时,它们会立即停止处理,从而提高效率。
312

被折叠的 条评论
为什么被折叠?



