From ceb4203d74c1312c4c669cd0464544506fcbcf42 Mon Sep 17 00:00:00 2001 From: AngusWu Date: Thu, 3 May 2018 23:30:57 +0800 Subject: [PATCH 01/12] Finish Quiz 2.1 --- .../java/lambdasinaction/chap2/Quiz2_1.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/main/java/lambdasinaction/chap2/Quiz2_1.java diff --git a/src/main/java/lambdasinaction/chap2/Quiz2_1.java b/src/main/java/lambdasinaction/chap2/Quiz2_1.java new file mode 100644 index 00000000..ca2dc870 --- /dev/null +++ b/src/main/java/lambdasinaction/chap2/Quiz2_1.java @@ -0,0 +1,68 @@ +package lambdasinaction.chap2; + +import java.util.Arrays; +import java.util.List; + +public class Quiz2_1 { + public static void main(String... args) { + List inventory = + Arrays.asList( + new Apple(80, "green"), + new Apple(155, "green"), + new Apple(120, "red")); + + prettyPrintApple(inventory, new AppleSimpleFormatter()); + } + + private static void prettyPrintApple(List inventory, AppleFormatter formatter) { + for (Apple apple : inventory) { + String output = formatter.accept(apple); + System.out.println(output); + } + } + + private static class AppleSimpleFormatter implements AppleFormatter { + + @Override + public String accept(Apple apple) { + return "An apple of " + apple.getWeight() + "g"; + } + } + + interface AppleFormatter { + String accept(Apple apple); + } + + private static class Apple { + private int weight = 0; + private String color = ""; + + public Apple(int weight, String color) { + this.weight = weight; + this.color = color; + } + + public Integer getWeight() { + return weight; + } + + public void setWeight(Integer weight) { + this.weight = weight; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String toString() { + return "Apple{" + + "color='" + color + '\'' + + ", weight=" + weight + + '}'; + } + } +} From a8e9350d967b95de9935a277d1f9359865eca02c Mon Sep 17 00:00:00 2001 From: AngusWu Date: Fri, 4 May 2018 00:13:08 +0800 Subject: [PATCH 02/12] Answer Quiz 2.2 --- src/main/java/lambdasinaction/chap2/MeaningOfThis.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/lambdasinaction/chap2/MeaningOfThis.java b/src/main/java/lambdasinaction/chap2/MeaningOfThis.java index 88dfb8e0..1efa70b5 100644 --- a/src/main/java/lambdasinaction/chap2/MeaningOfThis.java +++ b/src/main/java/lambdasinaction/chap2/MeaningOfThis.java @@ -18,6 +18,10 @@ public void run(){ public static void main(String...args) { MeaningOfThis m = new MeaningOfThis(); - m.doIt(); // ??? + m.doIt(); + // output is 5 + // Because the result of println which is comes from Runnable class + // Not MeaningOfThis class + // It is in different Level class } } From b7b7d66ea5140257d8ba29c387ad5f02632f8d06 Mon Sep 17 00:00:00 2001 From: AngusWu Date: Fri, 4 May 2018 00:44:09 +0800 Subject: [PATCH 03/12] Add anonymous for filterApple --- .../chap2/FilteringApplesWithAnonymous.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java diff --git a/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java b/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java new file mode 100644 index 00000000..ddbd7920 --- /dev/null +++ b/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java @@ -0,0 +1,67 @@ +package lambdasinaction.chap2; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class FilteringApplesWithAnonymous { + + public static void main(String... args) { + List inventory = Arrays.asList(new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red")); + + // [Apple{color='green', weight=155}] + List heavyApples = filter(inventory, new ApplePredicate() { + public boolean test(Apple apple) { + return apple.getWeight() > 150; + } + }); + System.out.println(heavyApples); + + // [Apple{color='red', weight=120}] + List redApples2 = filter(inventory, new ApplePredicate() { + public boolean test(Apple apple) { + return apple.getColor().equals("red"); + } + }); + System.out.println(redApples2); + } + + public static List filter(List inventory, ApplePredicate p) { + List result = new ArrayList<>(); + for (Apple apple : inventory) { + if (p.test(apple)) { + result.add(apple); + } + } + return result; + } + + public static class Apple { + private int weight = 0; + private String color = ""; + + public Apple(int weight, String color) { + this.weight = weight; + this.color = color; + } + + public Integer getWeight() { + return weight; + } + + public String getColor() { + return color; + } + + public String toString() { + return "Apple{" + + "color='" + color + '\'' + + ", weight=" + weight + + '}'; + } + } + + interface ApplePredicate { + boolean test(Apple a); + } +} \ No newline at end of file From 4583725754e0be0320dfcdbb1bb2cfbec2434762 Mon Sep 17 00:00:00 2001 From: AngusWu Date: Fri, 4 May 2018 00:44:42 +0800 Subject: [PATCH 04/12] Refactor: use lambda instead --- .../chap2/FilteringApplesWithAnonymous.java | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java b/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java index ddbd7920..b1769f59 100644 --- a/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java +++ b/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java @@ -7,22 +7,17 @@ public class FilteringApplesWithAnonymous { public static void main(String... args) { - List inventory = Arrays.asList(new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red")); + List inventory = Arrays.asList( + new Apple(80, "green"), + new Apple(155, "green"), + new Apple(120, "red")); // [Apple{color='green', weight=155}] - List heavyApples = filter(inventory, new ApplePredicate() { - public boolean test(Apple apple) { - return apple.getWeight() > 150; - } - }); + List heavyApples = filter(inventory, apple -> apple.getWeight() > 150); System.out.println(heavyApples); // [Apple{color='red', weight=120}] - List redApples2 = filter(inventory, new ApplePredicate() { - public boolean test(Apple apple) { - return apple.getColor().equals("red"); - } - }); + List redApples2 = filter(inventory, apple -> apple.getColor().equals("red")); System.out.println(redApples2); } @@ -37,19 +32,19 @@ public static List filter(List inventory, ApplePredicate p) { } public static class Apple { - private int weight = 0; - private String color = ""; + private int weight; + private String color; - public Apple(int weight, String color) { + Apple(int weight, String color) { this.weight = weight; this.color = color; } - public Integer getWeight() { + Integer getWeight() { return weight; } - public String getColor() { + String getColor() { return color; } From e79b11398509a059ba8618ad72ec36c063d39bbb Mon Sep 17 00:00:00 2001 From: AngusWu Date: Fri, 4 May 2018 10:19:42 +0800 Subject: [PATCH 05/12] using Generic and add Banana class for filter --- .../chap2/FilteringApplesWithAnonymous.java | 69 +++++++++++++++---- 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java b/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java index b1769f59..155b4d18 100644 --- a/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java +++ b/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java @@ -7,30 +7,77 @@ public class FilteringApplesWithAnonymous { public static void main(String... args) { - List inventory = Arrays.asList( + List inventoryOfApple = Arrays.asList( new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red")); + List inventoryOfBanana = Arrays.asList( + new Banana(80, "yellow"), + new Banana(155, "black"), + new Banana(120, "black")); + // [Apple{color='green', weight=155}] - List heavyApples = filter(inventory, apple -> apple.getWeight() > 150); + List heavyApples = filter(inventoryOfApple, apple -> apple.getWeight() > 150); System.out.println(heavyApples); + // [Banana{color='black', weight=155}] + List heavyBanana = filter(inventoryOfBanana, banana -> banana.getWeight() > 150); + System.out.println(heavyBanana); + // [Apple{color='red', weight=120}] - List redApples2 = filter(inventory, apple -> apple.getColor().equals("red")); - System.out.println(redApples2); + List redApples = filter(inventoryOfApple, apple -> apple.getColor().equals("red")); + System.out.println(redApples); + // [Banana{color='black', weight=155}, Banana{color='black', weight=120}] + List blackBanana = filter(inventoryOfBanana, banana -> banana.getColor().equals("black")); + System.out.println(blackBanana); + } + + public interface Predicate { + boolean test(T a); } - public static List filter(List inventory, ApplePredicate p) { - List result = new ArrayList<>(); - for (Apple apple : inventory) { - if (p.test(apple)) { - result.add(apple); + /* + Java doesn't know what type of T is. + Until I instantiate a type + Because this is a static method, + I have to tell what type of this static method is + */ + public static List filter(List inventory, Predicate p) { + List result = new ArrayList<>(); + for (T type : inventory) { + if (p.test(type)) { + result.add(type); } } return result; } + public static class Banana { + private int weight; + private String color; + + Banana(int weight, String color) { + this.weight = weight; + this.color = color; + } + + Integer getWeight() { + return weight; + } + + String getColor() { + return color; + } + + public String toString() { + return "Banana{" + + "color='" + color + '\'' + + ", weight=" + weight + + '}'; + } + } + public static class Apple { private int weight; private String color; @@ -55,8 +102,4 @@ public String toString() { '}'; } } - - interface ApplePredicate { - boolean test(Apple a); - } } \ No newline at end of file From 395462e41baa8b06fefdcce62a1fc37f35063db4 Mon Sep 17 00:00:00 2001 From: AngusWu Date: Fri, 11 May 2018 10:49:04 +0800 Subject: [PATCH 06/12] Add FunctionInterface --- .../chap3/SimpleFunInterfaceImpl.java | 16 ++++++++++++++++ .../chap3/SimpleFuncInterface.java | 6 ++++++ 2 files changed, 22 insertions(+) create mode 100644 src/main/java/lambdasinaction/chap3/SimpleFunInterfaceImpl.java create mode 100644 src/main/java/lambdasinaction/chap3/SimpleFuncInterface.java diff --git a/src/main/java/lambdasinaction/chap3/SimpleFunInterfaceImpl.java b/src/main/java/lambdasinaction/chap3/SimpleFunInterfaceImpl.java new file mode 100644 index 00000000..6a47af43 --- /dev/null +++ b/src/main/java/lambdasinaction/chap3/SimpleFunInterfaceImpl.java @@ -0,0 +1,16 @@ +package lambdasinaction.chap3; + +public class SimpleFunInterfaceImpl { + public static void main(String[] args) { + carryOutWork(new SimpleFuncInterface() { + @Override + public void doWork() { + System.out.println("Do work in SimpleFun impl..."); + } + }); + carryOutWork(() -> System.out.println("Do work in lambda exp impl...")); + } + public static void carryOutWork(SimpleFuncInterface sfi){ + sfi.doWork(); + } +} diff --git a/src/main/java/lambdasinaction/chap3/SimpleFuncInterface.java b/src/main/java/lambdasinaction/chap3/SimpleFuncInterface.java new file mode 100644 index 00000000..baa11e6b --- /dev/null +++ b/src/main/java/lambdasinaction/chap3/SimpleFuncInterface.java @@ -0,0 +1,6 @@ +package lambdasinaction.chap3; + +@FunctionalInterface +public interface SimpleFuncInterface { + void doWork(); +} From 1c59db84144b86af9dd5ed32f084232a3a1406be Mon Sep 17 00:00:00 2001 From: AngusWu Date: Fri, 11 May 2018 11:25:42 +0800 Subject: [PATCH 07/12] Make it runnable --- .../lambdasinaction/chap3/ExecuteAround.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/lambdasinaction/chap3/ExecuteAround.java b/src/main/java/lambdasinaction/chap3/ExecuteAround.java index 6c15fb6a..6ff51291 100644 --- a/src/main/java/lambdasinaction/chap3/ExecuteAround.java +++ b/src/main/java/lambdasinaction/chap3/ExecuteAround.java @@ -1,9 +1,13 @@ package lambdasinaction.chap3; -import java.io.*; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + public class ExecuteAround { + private static ClassLoader classLoader = ExecuteAround.class.getClassLoader(); - public static void main(String ...args) throws IOException{ + public static void main(String... args) throws IOException { // method we want to refactor to make more flexible String result = processFileLimited(); @@ -11,31 +15,31 @@ public static void main(String ...args) throws IOException{ System.out.println("---"); - String oneLine = processFile((BufferedReader b) -> b.readLine()); - System.out.println(oneLine); + String oneLine = processFile((BufferedReader b) -> b.readLine()); + System.out.println(oneLine); - String twoLines = processFile((BufferedReader b) -> b.readLine() + b.readLine()); - System.out.println(twoLines); + String twoLines = processFile((BufferedReader b) -> b.readLine() + b.readLine()); + System.out.println(twoLines); - } + } public static String processFileLimited() throws IOException { try (BufferedReader br = - new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))) { + new BufferedReader(new FileReader(classLoader.getResource("lambdasinaction/chap3/data.txt").getPath()))) { return br.readLine(); } } - public static String processFile(BufferedReaderProcessor p) throws IOException { - try(BufferedReader br = new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))){ - return p.process(br); - } + public static String processFile(BufferedReaderProcessor p) throws IOException { + try (BufferedReader br = new BufferedReader(new FileReader(classLoader.getResource("lambdasinaction/chap3/data.txt").getPath()))) { + return p.process(br); + } - } + } - public interface BufferedReaderProcessor{ - public String process(BufferedReader b) throws IOException; + public interface BufferedReaderProcessor { + public String process(BufferedReader b) throws IOException; - } + } } From 6a63384193282a06a7c38b819ca1903d786605a3 Mon Sep 17 00:00:00 2001 From: AngusWu Date: Fri, 11 May 2018 11:26:03 +0800 Subject: [PATCH 08/12] Prepare to refactor --- .../chap3/SelfDoExecuteAround.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java diff --git a/src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java b/src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java new file mode 100644 index 00000000..bfa81652 --- /dev/null +++ b/src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java @@ -0,0 +1,21 @@ +package lambdasinaction.chap3; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class SelfDoExecuteAround { + private static ClassLoader classLoader = ExecuteAround.class.getClassLoader(); + + public static void main(String... args) throws IOException { + String result = processFile(); + System.out.println(result); + } + + private static String processFile() throws IOException { + try (BufferedReader reader = new BufferedReader( + new FileReader(classLoader.getResource("lambdasinaction/chap3/data.txt").getPath()))) { + return reader.readLine(); + } + } +} From e2caf4b8dcad6841d476d7f32496d94911fa5e5e Mon Sep 17 00:00:00 2001 From: AngusWu Date: Fri, 11 May 2018 11:49:06 +0800 Subject: [PATCH 09/12] Use lambda and interface let code more flexiable --- .../chap3/SelfDoExecuteAround.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java b/src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java index bfa81652..14a62451 100644 --- a/src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java +++ b/src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java @@ -8,14 +8,21 @@ public class SelfDoExecuteAround { private static ClassLoader classLoader = ExecuteAround.class.getClassLoader(); public static void main(String... args) throws IOException { - String result = processFile(); - System.out.println(result); + String oneLine = processFile((BufferedReader br) -> br.readLine()); + System.out.println("This is one line:\n" + oneLine); + + String twoLines = processFile((BufferedReader br) -> br.readLine() + br.readLine()); + System.out.println("This is two line:\n" + twoLines); } - private static String processFile() throws IOException { + private static String processFile(BufferedReaderProcessor processor) throws IOException { try (BufferedReader reader = new BufferedReader( new FileReader(classLoader.getResource("lambdasinaction/chap3/data.txt").getPath()))) { - return reader.readLine(); + return processor.process(reader); } } + + interface BufferedReaderProcessor { + String process(BufferedReader reader) throws IOException; + } } From 0089312874e60a7a5ead1a507ce9b967b3342f70 Mon Sep 17 00:00:00 2001 From: AngusWu Date: Fri, 11 May 2018 13:43:53 +0800 Subject: [PATCH 10/12] Practice Consumer --- .../lambdasinaction/chap3/UseConsumer.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/lambdasinaction/chap3/UseConsumer.java diff --git a/src/main/java/lambdasinaction/chap3/UseConsumer.java b/src/main/java/lambdasinaction/chap3/UseConsumer.java new file mode 100644 index 00000000..98b23460 --- /dev/null +++ b/src/main/java/lambdasinaction/chap3/UseConsumer.java @@ -0,0 +1,18 @@ +package lambdasinaction.chap3; + +import java.util.Arrays; +import java.util.List; +import java.util.function.Consumer; + +public class UseConsumer { + public static void main(String... args) { + forEach(Arrays.asList("1", "2", "3", "4", "5"), + (String output) -> System.out.println(output)); + } + + private static void forEach(List list, Consumer consumer) { + for (T i : list) { + consumer.accept(i); + } + } +} From 73446f3b30d73ba0d23924d2877fa0856d82f5de Mon Sep 17 00:00:00 2001 From: AngusWu Date: Fri, 11 May 2018 14:05:49 +0800 Subject: [PATCH 11/12] Practice Function --- pom.xml | 5 ++++ .../lambdasinaction/chap3/UseFunction.java | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/main/java/lambdasinaction/chap3/UseFunction.java diff --git a/pom.xml b/pom.xml index 10e5035e..1818be16 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,11 @@ junit 4.11 + + com.google.code.gson + gson + 2.8.2 + diff --git a/src/main/java/lambdasinaction/chap3/UseFunction.java b/src/main/java/lambdasinaction/chap3/UseFunction.java new file mode 100644 index 00000000..87ce1064 --- /dev/null +++ b/src/main/java/lambdasinaction/chap3/UseFunction.java @@ -0,0 +1,24 @@ +package lambdasinaction.chap3; + +import com.google.gson.Gson; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.Function; + +public class UseFunction { + public static void main(String... args) { + List list = map(Arrays.asList("lambdas", "in", "action"), + (String item) -> item.length()); + System.out.println(new Gson().toJson(list)); + } + + private static List map(List list, Function function) { + List result = new ArrayList<>(); + for (T item : list) { + result.add(function.apply(item)); + } + return result; + } +} From 533797baf040ef9c6eedfff7e25b3a26917e6a6b Mon Sep 17 00:00:00 2001 From: AngusWu Date: Sun, 29 Jul 2018 16:04:42 +0800 Subject: [PATCH 12/12] Practicing Lambda --- .../java/lambdasinaction/chap3/Lambdas.java | 157 ++++++++++-------- 1 file changed, 89 insertions(+), 68 deletions(-) diff --git a/src/main/java/lambdasinaction/chap3/Lambdas.java b/src/main/java/lambdasinaction/chap3/Lambdas.java index 697f340e..a6776bdc 100644 --- a/src/main/java/lambdasinaction/chap3/Lambdas.java +++ b/src/main/java/lambdasinaction/chap3/Lambdas.java @@ -1,73 +1,94 @@ package lambdasinaction.chap3; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.ToIntBiFunction; + +import static java.util.Comparator.comparing; public class Lambdas { - public static void main(String ...args){ - - // Simple example - Runnable r = () -> System.out.println("Hello!"); - r.run(); - - // Filtering with lambdas - List inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red")); - - // [Apple{color='green', weight=80}, Apple{color='green', weight=155}] - List greenApples = filter(inventory, (Apple a) -> "green".equals(a.getColor())); - System.out.println(greenApples); - - - Comparator c = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()); - - // [Apple{color='green', weight=80}, Apple{color='red', weight=120}, Apple{color='green', weight=155}] - inventory.sort(c); - System.out.println(inventory); - } - - public static List filter(List inventory, ApplePredicate p){ - List result = new ArrayList<>(); - for(Apple apple : inventory){ - if(p.test(apple)){ - result.add(apple); - } - } - return result; - } - - public static class Apple { - private int weight = 0; - private String color = ""; - - public Apple(int weight, String color){ - this.weight = weight; - this.color = color; - } - - public Integer getWeight() { - return weight; - } - - public void setWeight(Integer weight) { - this.weight = weight; - } - - public String getColor() { - return color; - } - - public void setColor(String color) { - this.color = color; - } - - public String toString() { - return "Apple{" + - "color='" + color + '\'' + - ", weight=" + weight + - '}'; - } - } - - interface ApplePredicate{ - public boolean test(Apple a); - } + public static void main(String... args) { + + // Simple example + Runnable r = () -> System.out.println("Hello!"); + r.run(); + + // Filtering with lambdas + List inventory = Arrays.asList(new Apple(80, "green", 100), new Apple(155, "green", 160), + new Apple(120, "red", 125), new Apple(80, "green", 90)); + + // [Apple{color='green', weight=80}, Apple{color='green', weight=155}] + List greenApples = filter(inventory, (Apple a) -> "green".equals(a.getColor())); + List greenApples1 = filter(inventory, a -> "green".equals(a.getColor())); + System.out.println(greenApples); + + + Comparator c = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()); + + // [Apple{color='green', weight=80}, Apple{color='red', weight=120}, Apple{color='green', weight=155}] + inventory.sort(c); + System.out.println(inventory); + + Comparator c1 = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()); + ToIntBiFunction c2 = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()); + BiFunction c3 = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()); + + // [Apple{color='green', weight=155}, Apple{color='red', weight=120}, Apple{color='green', weight=80}] + inventory.sort(comparing(Apple::getWeight).reversed()); + System.out.println(inventory); + + inventory.sort(comparing(Apple::getWeight) + .reversed() + .thenComparing(Apple::getPrice)); + System.out.println(inventory); + } + + public static List filter(List inventory, ApplePredicate p) { + List result = new ArrayList<>(); + for (Apple apple : inventory) { + if (p.test(apple)) { + result.add(apple); + } + } + return result; + } + + public static class Apple { + private int weight = 0; + private String color = ""; + private int price = 0; + + public Apple(int weight, String color, int price) { + this.weight = weight; + this.color = color; + this.price = price; + } + + public Integer getWeight() { + return weight; + } + + public String getColor() { + return color; + } + + public int getPrice() { + return price; + } + + public String toString() { + return "Apple{" + + "color='" + color + '\'' + + ", weight=" + weight + + ", price=" + price + + '}'; + } + } + + interface ApplePredicate { + public boolean test(Apple a); + } } \ No newline at end of file