Skip to content

Commit

Permalink
Refactor Predicate and Consumer sections for better readability
Browse files Browse the repository at this point in the history
Refactored the Predicate and Consumer sections in `StringExercises.java`, and added debugging outputs in `SumBigDecimals.java` and `LoopsSortsAndIfs.java`. This improves debugging capabilities and readability of the code base.

In `StringExercises`, now using a lambda expression to handle logger.info for better control. This modification makes code more self-explaining and increases the predictability of the logger system.

In `SumBigDecimals`, added debug print statements in the reduce method, making it easier to trace the execution of the computation and to spot potential issues.

In `LoopsSortsAndIfs`, the code became more readable and maintainable by using stream api, the code has been modified to replace traditional loops and conditionals with a single stream pipeline. This change improves readability and maintainability.
  • Loading branch information
kousen committed Oct 4, 2023
1 parent cfa5603 commit e25dd5d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
29 changes: 7 additions & 22 deletions src/main/java/refactoring/after/LoopsSortsAndIfs.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
package refactoring.after;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class LoopsSortsAndIfs {
public static void main(String[] args) {
String[] strings = "this is an array of strings".split(" ");

List<String> evenLengths = new ArrayList<>();
for (String s : strings) {
if (s.length() % 2 == 0) {
evenLengths.add(s.toUpperCase());
}
}

Collections.sort(evenLengths, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
});

for (String s : evenLengths) {
System.out.println(s);
}
Arrays.stream("this is an array of strings".split(" "))
.filter(s -> s.length() % 2 == 0)
.map(String::toUpperCase)
.sorted(Comparator.comparingInt(String::length)
.thenComparing(Comparator.naturalOrder()))
.forEach(System.out::println);
}
}
5 changes: 4 additions & 1 deletion src/main/java/streams/SumBigDecimals.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public BigDecimal sumFirstN_asDoubles(int n) {
public BigDecimal sumFirstN_usingReduce(int n) {
return Stream.iterate(BigDecimal.ONE, bd -> bd.add(BigDecimal.ONE))
.limit(n)
.reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
.reduce((total, element) -> {
System.out.println("total = " + total + ", element = " + element);
return total.add(element);
}).orElse(BigDecimal.ZERO);
}

// Off by one error, because 1 is never doubled
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/streams/StringExercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void demoCollectors() {
Predicate<String> nonNull = Objects::nonNull;
Predicate<String> evenLength = s -> s.length() % 2 == 0;

Consumer<String> log = logger::info;
Consumer<String> log = msg -> logger.info(() -> msg);
Consumer<Object> print = System.out::println;

// Combine the two predicates and use the result to print non-null, even-length strings
Expand Down

0 comments on commit e25dd5d

Please sign in to comment.