-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringPredicateExample.java
43 lines (33 loc) · 1.22 KB
/
StringPredicateExample.java
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
package org.athenian._4_predicates;
import java.util.Arrays;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class StringPredicateExample {
public static void main(String[] args) {
// Verbose
Predicate<String> containsHelloVerbose =
new Predicate<>() {
@Override
public boolean test(String val) {
return val.contains("Hello");
}
};
// Better
Predicate<String> containsHelloMedium = (String val) -> {
return val.contains("Hello");
};
// Terse
Predicate<String> containsHelloTerse = val -> val.contains("Hello");
System.out.println(containsHelloTerse.test("Hello"));
System.out.println(containsHelloTerse.test("Goodbye"));
var names = Arrays.asList("Alice", "Bill", "Allicia", "Pete");
var a_names = names.stream()
.filter(val -> val.startsWith("A"))
.collect(Collectors.toList());
var non_a_names = names.stream()
.filter(val -> !a_names.contains(val))
.collect(Collectors.toList());
System.out.println("A names: " + a_names);
System.out.println("Non A names: " + non_a_names);
}
}