diff --git a/src/main/java/io/ProcessDictionary.java b/src/main/java/io/ProcessDictionary.java index 91aaeec..ab28e04 100644 --- a/src/main/java/io/ProcessDictionary.java +++ b/src/main/java/io/ProcessDictionary.java @@ -21,7 +21,8 @@ public class ProcessDictionary { public long getMaxLength() { try (Stream 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); } @@ -29,15 +30,14 @@ public long getMaxLength() { public void printTenLongestWords() { System.out.println("\nTen Longest Words:"); - long max = getMaxLength() - 5; + long max = getMaxLength() - 10; try (Stream 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); } diff --git a/src/main/java/refactoring/after/LoopsSortsAndIfs.java b/src/main/java/refactoring/after/LoopsSortsAndIfs.java index 4b63ff1..d29482e 100644 --- a/src/main/java/refactoring/after/LoopsSortsAndIfs.java +++ b/src/main/java/refactoring/after/LoopsSortsAndIfs.java @@ -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 evenLengths = new ArrayList<>(); - for (String s : strings) { - if (s.length() % 2 == 0) { - evenLengths.add(s.toUpperCase()); - } - } - - Collections.sort(evenLengths, new Comparator() { - @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); } } diff --git a/src/main/java/streams/SumBigDecimals.java b/src/main/java/streams/SumBigDecimals.java index 8451738..2ed8069 100644 --- a/src/main/java/streams/SumBigDecimals.java +++ b/src/main/java/streams/SumBigDecimals.java @@ -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