From 582b56a92ca5bb6e2d90e874ef782cbc7f3df669 Mon Sep 17 00:00:00 2001 From: Abhinav Nath Date: Wed, 15 Jun 2022 22:02:30 +0530 Subject: [PATCH] Refactor code --- .../functionalinterfaces/ComparatorLambdaExample.java | 2 +- .../designpatterns/decorator/example2/BurgerShop.java | 2 +- .../designpatterns/iterator/MyArrayList.java | 4 ++-- .../codecafe/java8/hiddentreasures/PureFunctions.java | 10 ++-------- .../codecafe/java8/lambdas/foundation/SortStrings.java | 2 +- .../foundation/exercise/Java7Style_Solution_1.java | 2 +- .../foundation/exercise/Java7Style_Solution_2.java | 2 +- 7 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/com/codecafe/java8/functionalinterfaces/ComparatorLambdaExample.java b/src/com/codecafe/java8/functionalinterfaces/ComparatorLambdaExample.java index 6f7e133..b6339e6 100644 --- a/src/com/codecafe/java8/functionalinterfaces/ComparatorLambdaExample.java +++ b/src/com/codecafe/java8/functionalinterfaces/ComparatorLambdaExample.java @@ -7,7 +7,7 @@ public class ComparatorLambdaExample { public static void main(String[] args) { // prior to Java 8 - Comparator comparator = new Comparator() { + Comparator comparator = new Comparator<>() { @Override public int compare(Integer arg0, Integer arg1) { diff --git a/src/com/codecafe/java8/functionalprogramming/designpatterns/decorator/example2/BurgerShop.java b/src/com/codecafe/java8/functionalprogramming/designpatterns/decorator/example2/BurgerShop.java index 5f49d74..657e0c1 100644 --- a/src/com/codecafe/java8/functionalprogramming/designpatterns/decorator/example2/BurgerShop.java +++ b/src/com/codecafe/java8/functionalprogramming/designpatterns/decorator/example2/BurgerShop.java @@ -5,7 +5,7 @@ // it will do the job of adding the decoration public class BurgerShop { - Function decoration; + private final Function decoration; public BurgerShop(Function decoration) { this.decoration = decoration; diff --git a/src/com/codecafe/java8/functionalprogramming/designpatterns/iterator/MyArrayList.java b/src/com/codecafe/java8/functionalprogramming/designpatterns/iterator/MyArrayList.java index d35732b..c0df28f 100644 --- a/src/com/codecafe/java8/functionalprogramming/designpatterns/iterator/MyArrayList.java +++ b/src/com/codecafe/java8/functionalprogramming/designpatterns/iterator/MyArrayList.java @@ -11,8 +11,8 @@ public MyArrayList(Object[] elements) { } public void forEach(Consumer action) { - for (int i = 0; i < elements.length; i++) - action.accept(elements[i]); + for (Object element : elements) + action.accept(element); } } \ No newline at end of file diff --git a/src/com/codecafe/java8/hiddentreasures/PureFunctions.java b/src/com/codecafe/java8/hiddentreasures/PureFunctions.java index 8a54b44..973dd7b 100644 --- a/src/com/codecafe/java8/hiddentreasures/PureFunctions.java +++ b/src/com/codecafe/java8/hiddentreasures/PureFunctions.java @@ -21,10 +21,7 @@ public static void main(String[] args) { public static boolean isGT3(int number) { System.out.println("isGT3 called for number " + number); - if (number > 3) - return true; - else - return false; + return number > 3; } public static int doubleIt(int number) { @@ -34,10 +31,7 @@ public static int doubleIt(int number) { public static boolean isEven(int number) { System.out.println("isEven called for number " + number); - if (number % 2 == 0) - return true; - else - return false; + return number % 2 == 0; } } \ No newline at end of file diff --git a/src/com/codecafe/java8/lambdas/foundation/SortStrings.java b/src/com/codecafe/java8/lambdas/foundation/SortStrings.java index fb07ed2..4670785 100644 --- a/src/com/codecafe/java8/lambdas/foundation/SortStrings.java +++ b/src/com/codecafe/java8/lambdas/foundation/SortStrings.java @@ -18,7 +18,7 @@ public static void main(String[] args) { List words1 = Arrays.asList("ab", "abcd", "abc", "abcde", "a", "abcdef"); // clumsy anonymous inner class method - Collections.sort(words1, new Comparator() { + Collections.sort(words1, new Comparator<>() { @Override public int compare(String s1, String s2) { return Integer.compare(s1.length(), s2.length()); diff --git a/src/com/codecafe/java8/lambdas/foundation/exercise/Java7Style_Solution_1.java b/src/com/codecafe/java8/lambdas/foundation/exercise/Java7Style_Solution_1.java index 803f8e9..795234d 100644 --- a/src/com/codecafe/java8/lambdas/foundation/exercise/Java7Style_Solution_1.java +++ b/src/com/codecafe/java8/lambdas/foundation/exercise/Java7Style_Solution_1.java @@ -22,7 +22,7 @@ public static void main(String[] args) { ); // Sort list by last name - Collections.sort(people, new Comparator() { + Collections.sort(people, new Comparator<>() { @Override public int compare(Person p1, Person p2) { diff --git a/src/com/codecafe/java8/lambdas/foundation/exercise/Java7Style_Solution_2.java b/src/com/codecafe/java8/lambdas/foundation/exercise/Java7Style_Solution_2.java index f25a24f..d3af0a1 100644 --- a/src/com/codecafe/java8/lambdas/foundation/exercise/Java7Style_Solution_2.java +++ b/src/com/codecafe/java8/lambdas/foundation/exercise/Java7Style_Solution_2.java @@ -22,7 +22,7 @@ public static void main(String[] args) { ); // Sort list by last name - Collections.sort(people, new Comparator() { + Collections.sort(people, new Comparator<>() { @Override public int compare(Person p1, Person p2) {