2015-11-14 16:18:05 -08:00
|
|
|
// streams/Matching.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2015-11-14 16:18:05 -08:00
|
|
|
// Demonstrates short-circuiting of *Match() operations
|
|
|
|
import java.util.stream.*;
|
|
|
|
import java.util.function.*;
|
|
|
|
import static streams.RandInts.*;
|
|
|
|
|
|
|
|
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, 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
|
|
|
|
*/
|