forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
53 lines (39 loc) · 2.05 KB
/
Main.java
File metadata and controls
53 lines (39 loc) · 2.05 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
package modern.challenge;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Melon> melons = Arrays.asList(
new Melon("Gac", 5500, "Europe"), new Melon("Bailan", 6000, "China"),
new Melon("Watermelon", 1200, "Europe"), new Melon("Gac", 3400, "US"),
new Melon("Bailan", 1300, "China"));
List<Melon> bailans = Filters.filterByType(melons, "Bailan");
System.out.println("Bailans: " + bailans);
List<Melon> melonsOf1200g = Filters.filterByWeight(melons, 1200);
System.out.println("Melons of 1200 grams: " + melonsOf1200g);
List<Melon> bailansOf1300g = Filters.filterByTypeAndWeight(melons, "Bailan", 1300);
System.out.println("Bailans of 1300 grams: " + bailansOf1300g);
List<Melon> gacs = Filters.filterMelons(melons, new GacMelonPredicate());
List<Melon> huge = Filters.filterMelons(melons, new HugeMelonPredicate());
System.out.println("Gacs: " + gacs);
System.out.println("Huge: " + huge);
List<Melon> europeans = Filters
.filterMelons(melons, new MelonPredicate() {
@Override
public boolean test(Melon melon) {
return "europe".equalsIgnoreCase(melon.getOrigin());
}
});
System.out.println("Europeans: " + europeans);
List<Melon> europeansLambda = Filters
.filterMelons(melons, m -> "europe".equalsIgnoreCase(m.getOrigin()));
System.out.println("Europeans (via lambda): " + europeansLambda);
List<Melon> watermelons = Filters
.filter(melons, (Melon m) -> "Watermelon".equalsIgnoreCase(m.getType()));
System.out.println("Watermelons: " + watermelons);
List<Integer> numbers = Arrays.asList(1, 13, 15, 2, 67);
List<Integer> smallThan10 = Filters
.filter(numbers, (Integer i) -> i < 10);
System.out.println("Numbers < 10: " + smallThan10);
}
}