Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav-nath committed Jun 15, 2022
1 parent e84e302 commit 582b56a
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class ComparatorLambdaExample {
public static void main(String[] args) {

// prior to Java 8
Comparator<Integer> comparator = new Comparator<Integer>() {
Comparator<Integer> comparator = new Comparator<>() {

@Override
public int compare(Integer arg0, Integer arg1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// it will do the job of adding the decoration
public class BurgerShop {

Function<Burger, Burger> decoration;
private final Function<Burger, Burger> decoration;

public BurgerShop(Function<Burger, Burger> decoration) {
this.decoration = decoration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public MyArrayList(Object[] elements) {
}

public void forEach(Consumer<Object> action) {
for (int i = 0; i < elements.length; i++)
action.accept(elements[i]);
for (Object element : elements)
action.accept(element);
}

}
10 changes: 2 additions & 8 deletions src/com/codecafe/java8/hiddentreasures/PureFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

}
2 changes: 1 addition & 1 deletion src/com/codecafe/java8/lambdas/foundation/SortStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void main(String[] args) {
List<String> words1 = Arrays.asList("ab", "abcd", "abc", "abcde", "a", "abcdef");

// clumsy anonymous inner class method
Collections.sort(words1, new Comparator<String>() {
Collections.sort(words1, new Comparator<>() {
@Override
public int compare(String s1, String s2) {
return Integer.compare(s1.length(), s2.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void main(String[] args) {
);

// Sort list by last name
Collections.sort(people, new Comparator<Person>() {
Collections.sort(people, new Comparator<>() {

@Override
public int compare(Person p1, Person p2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void main(String[] args) {
);

// Sort list by last name
Collections.sort(people, new Comparator<Person>() {
Collections.sort(people, new Comparator<>() {

@Override
public int compare(Person p1, Person p2) {
Expand Down

0 comments on commit 582b56a

Please sign in to comment.