Skip to content

Commit

Permalink
Refactor loops and enhance logging in various classes
Browse files Browse the repository at this point in the history
Refactor `LoopsSortsAndIfs.java` by using streams to simplify the code. Add detailed logging in `SumBigDecimals.java` for debugging purposes. Adjust the filtering logic in `ProcessDictionary.java` to change the maximum length threshold for printing the longest words.
  • Loading branch information
kousen committed Sep 18, 2024
1 parent 33bfd02 commit 9351381
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/main/java/io/ProcessDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ public class ProcessDictionary {

public long getMaxLength() {
try (Stream<String> words = Files.lines(dictionary)) {
return words.mapToInt(String::length).max().orElse(0);
return words.mapToInt(String::length)
.max().orElse(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public void printTenLongestWords() {
System.out.println("\nTen Longest Words:");
long max = getMaxLength() - 5;
long max = getMaxLength() - 10;
try (Stream<String> words = Files.lines(dictionary)) {
words.filter(s -> s.length() > max)
.sorted(Comparator.comparingInt(String::length).reversed()
//.thenComparing(Comparator.reverseOrder()))
)
.limit(10)
.forEach(w ->
System.out.printf("%s (%d)%n", w, w.length()));
.forEach(w -> System.out.printf("%s (%d)%n", w, w.length()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
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

0 comments on commit 9351381

Please sign in to comment.