1+ import java .util .*;
2+ public class FilteringApples {
3+
4+ public static void main (String ... args ){
5+
6+ List <Apple > inventory = Arrays .asList (new Apple (80 ,"green" ), new Apple (155 , "green" ), new Apple (120 , "red" ));
7+
8+ // [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
9+ List <Apple > greenApples = filterApplesByColor (inventory , "green" );
10+ System .out .println (greenApples );
11+
12+ // [Apple{color='red', weight=120}]
13+ List <Apple > redApples = filterApplesByColor (inventory , "red" );
14+ System .out .println (redApples );
15+
16+ // [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
17+ List <Apple > greenApples2 = filter (inventory , new AppleColorPredicate ());
18+ System .out .println (greenApples2 );
19+
20+ // [Apple{color='green', weight=155}]
21+ List <Apple > heavyApples = filter (inventory , new AppleWeightPredicate ());
22+ System .out .println (heavyApples );
23+
24+ // []
25+ List <Apple > redAndHeavyApples = filter (inventory , new AppleRedAndHeavyPredicate ());
26+ System .out .println (redAndHeavyApples );
27+
28+ // [Apple{color='red', weight=120}]
29+ List <Apple > redApples2 = filter (inventory , new ApplePredicate () {
30+ public boolean test (Apple a ){
31+ return a .getColor ().equals ("red" );
32+ }
33+ });
34+ System .out .println (redApples2 );
35+
36+ }
37+
38+ public static List <Apple > filterGreenApples (List <Apple > inventory ){
39+ List <Apple > result = new ArrayList <>();
40+ for (Apple apple : inventory ){
41+ if ("green" .equals (apple .getColor ())){
42+ result .add (apple );
43+ }
44+ }
45+ return result ;
46+ }
47+
48+ public static List <Apple > filterApplesByColor (List <Apple > inventory , String color ){
49+ List <Apple > result = new ArrayList <>();
50+ for (Apple apple : inventory ){
51+ if (apple .getColor ().equals (color )){
52+ result .add (apple );
53+ }
54+ }
55+ return result ;
56+ }
57+
58+ public static List <Apple > filterApplesByWeight (List <Apple > inventory , int weight ){
59+ List <Apple > result = new ArrayList <>();
60+ for (Apple apple : inventory ){
61+ if (apple .getWeight () > weight ){
62+ result .add (apple );
63+ }
64+ }
65+ return result ;
66+ }
67+
68+
69+ public static List <Apple > filter (List <Apple > inventory , ApplePredicate p ){
70+ List <Apple > result = new ArrayList <>();
71+ for (Apple apple : inventory ){
72+ if (p .test (apple )){
73+ result .add (apple );
74+ }
75+ }
76+ return result ;
77+ }
78+
79+ public static class Apple {
80+ private int weight = 0 ;
81+ private String color = "" ;
82+
83+ public Apple (int weight , String color ){
84+ this .weight = weight ;
85+ this .color = color ;
86+ }
87+
88+ public Integer getWeight () {
89+ return weight ;
90+ }
91+
92+ public void setWeight (Integer weight ) {
93+ this .weight = weight ;
94+ }
95+
96+ public String getColor () {
97+ return color ;
98+ }
99+
100+ public void setColor (String color ) {
101+ this .color = color ;
102+ }
103+
104+ public String toString () {
105+ return "Apple{" +
106+ "color='" + color + '\'' +
107+ ", weight=" + weight +
108+ '}' ;
109+ }
110+ }
111+
112+ interface ApplePredicate {
113+ public boolean test (Apple a );
114+ }
115+
116+ static class AppleWeightPredicate implements ApplePredicate {
117+ public boolean test (Apple apple ){
118+ return apple .getWeight () > 150 ;
119+ }
120+ }
121+ static class AppleColorPredicate implements ApplePredicate {
122+ public boolean test (Apple apple ){
123+ return "green" .equals (apple .getColor ());
124+ }
125+ }
126+
127+ static class AppleRedAndHeavyPredicate implements ApplePredicate {
128+ public boolean test (Apple apple ){
129+ return "red" .equals (apple .getColor ())
130+ && apple .getWeight () > 150 ;
131+ }
132+ }
133+ }
0 commit comments