-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredicateAndPredicateJoiningExample.java
More file actions
63 lines (48 loc) · 1.7 KB
/
PredicateAndPredicateJoiningExample.java
File metadata and controls
63 lines (48 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package predefined_functional_interfaces.predicate;
/*
* Example demonstrating use of Predicates and Predicate Joining.
*/
import java.util.function.Predicate;
public class PredicateAndPredicateJoiningExample {
// Predicate to check a number is greater than 10 or not.
static Predicate<Integer> checkGreaterThanTen = i -> i>10;
// Predicate to check a number is even or not
static Predicate<Integer> checkEven = i -> i%2==0;
public static void main(String[] args) {
int[] arr = {0, 5, 10, 15, 20, 25, 30};
System.out.println("Numbers greater than 10:");
printConditionally(checkGreaterThanTen, arr);
System.out.println("Even numbers:");
printConditionally(checkEven, arr);
//negate() -- reverses the condition of predicate
System.out.println("Numbers NOT greater than 10:");
printConditionally(checkGreaterThanTen.negate(), arr);
//and()
System.out.println("Numbers greater than 10 AND Even:");
printConditionally(checkGreaterThanTen.and(checkEven), arr);
//or()
System.out.println("Numbers greater than 10 OR Even:");
printConditionally(checkGreaterThanTen.or(checkEven), arr);
}
public static void printConditionally(Predicate<Integer>p, int[] arr) {
System.out.print("[");
for(int i : arr) {
if(p.test(i))
System.out.print(i+",");
}
System.out.println("]");
}
}
/*
* Output:
* Numbers greater than 10:
* [15,20,25,30,]
* Even numbers:
* [0,10,20,30,]
* Numbers NOT greater than 10:
* [0,5,10,]
* Numbers greater than 10 AND Even:
* [20,30,]
* Numbers greater than 10 OR Even:
* [0,10,15,20,25,30,]
*/