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/chap2/FilteringApplesWithAnonymous.java b/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java new file mode 100644 index 00000000..155b4d18 --- /dev/null +++ b/src/main/java/lambdasinaction/chap2/FilteringApplesWithAnonymous.java @@ -0,0 +1,105 @@ +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 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(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 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); + } + + /* + 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; + + Apple(int weight, String color) { + this.weight = weight; + this.color = color; + } + + Integer getWeight() { + return weight; + } + + String getColor() { + return color; + } + + public String toString() { + return "Apple{" + + "color='" + color + '\'' + + ", weight=" + weight + + '}'; + } + } +} \ No newline at end of file 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 } } 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 + + '}'; + } + } +} 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; - } + } } 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 diff --git a/src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java b/src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java new file mode 100644 index 00000000..14a62451 --- /dev/null +++ b/src/main/java/lambdasinaction/chap3/SelfDoExecuteAround.java @@ -0,0 +1,28 @@ +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 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(BufferedReaderProcessor processor) throws IOException { + try (BufferedReader reader = new BufferedReader( + new FileReader(classLoader.getResource("lambdasinaction/chap3/data.txt").getPath()))) { + return processor.process(reader); + } + } + + interface BufferedReaderProcessor { + String process(BufferedReader reader) throws IOException; + } +} 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(); +} 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); + } + } +} 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; + } +}