-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredicateBasics.java
More file actions
133 lines (97 loc) · 4.36 KB
/
predicateBasics.java
File metadata and controls
133 lines (97 loc) · 4.36 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.home;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class predicateBasics {
public static void main(String[] args) {
//In Java 8, Predicate is a functional interface, which accepts an argument and returns a boolean.
// Usually, it used to apply in a filter for a collection of objects.
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> collect = list.stream().filter(x -> x > 5).collect(Collectors.toList());
System.out.println(collect); // [6, 7, 8, 9, 10]
//predicate
Predicate<Integer> noGreaterThan5 = x -> x > 5;
List<Integer> lt = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> collect1 = lt.stream()
.filter(noGreaterThan5)
.collect(Collectors.toList());
System.out.println(collect1); // [6, 7, 8, 9, 10]
//2. Predicate.and()
//2.1 Multiple filters.
List<Integer> lists = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// multiple filters
List<Integer> collects = lists.stream()
.filter(x -> x > 5 && x < 8).collect(Collectors.toList());
System.out.println(collects);
Predicate<Integer> noGreaterThan5_ = x -> x > 5;
Predicate<Integer> noLessThan8 = x -> x < 8;
List<Integer> listss = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> collectss = listss.stream()
.filter(noGreaterThan5_.and(noLessThan8))
.collect(Collectors.toList());
System.out.println(collectss);
//OR
Predicate<String> lengthIs3 = x -> x.length() == 3;
Predicate<String> startWithA = x -> x.startsWith("A");
List<String> orL = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB");
List<String> orcollect = orL.stream()
.filter(lengthIs3.or(startWithA))
.collect(Collectors.toList());
System.out.println(orcollect);//[A, AA, AAA, BBB]
//negate
Predicate<String> startWithANeg = x -> x.startsWith("A");
List<String> neglist = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB");
List<String> collectneg = neglist.stream()
.filter(startWithANeg.negate())
.collect(Collectors.toList());
System.out.println(collectneg);//[B, BB, BBB]
//5. Predicate.test() in function
List<String> listTest = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB");
System.out.println(StringProcessor.filter(
listTest, x -> x.startsWith("A"))); // [A, AA, AAA]
System.out.println(StringProcessor.filter(
listTest, x -> x.startsWith("A") && x.length() == 3)); // [AAA]
//chaining
Predicate<String> startWithAChain = x -> x.startsWith("a");
// start with "a" or "m"
boolean result = startWithAChain.or(x -> x.startsWith("k")).test("kee");
System.out.println(result); // true
// !(start with "a" and length is 3)
boolean result2 = startWithAChain.and(x -> x.length() == 3).negate().test("abc");
System.out.println(result2);
//more way
List<Predicate<String>> allPredicates = new ArrayList<Predicate<String>>();
allPredicates.add(str -> str.startsWith("A"));
allPredicates.add(str -> str.contains("d"));
allPredicates.add(str -> str.length() > 4);
List<String> names = Arrays.asList("Adam", "Alexander", "John", "Tom");
List<String> results = names.stream()
.filter(allPredicates.stream().reduce(x -> true, Predicate::and))
.collect(Collectors.toList());
System.out.println(results);
List<String> resultss = names.stream()
.filter(allPredicates.stream().reduce(x -> false, Predicate::or))
.collect(Collectors.toList());
System.out.println(resultss);
}
static class StringProcessor {
static List<String> filter(List<String> list, Predicate<String> predicate) {
return list.stream().filter(predicate::test).collect(Collectors.toList());
}
}
/*
[6, 7, 8, 9, 10]
[6, 7, 8, 9, 10]
[6, 7]
[6, 7]
[A, AA, AAA, BBB]
[B, BB, BBB]
[A, AA, AAA]
[AAA]
true
false
[Alexander]
[Adam, Alexander]
*/
}