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