diff --git a/src/main/java/datetime/AntarcticaTimeZones.java b/src/main/java/datetime/AntarcticaTimeZones.java index 9ba5e6f..0daec82 100644 --- a/src/main/java/datetime/AntarcticaTimeZones.java +++ b/src/main/java/datetime/AntarcticaTimeZones.java @@ -4,7 +4,6 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.List; -import java.util.stream.Collectors; import static java.util.Comparator.comparingInt; @@ -18,7 +17,7 @@ public static void main(String[] args) { .map(now::atZone) // Stream .sorted(comparingInt( zoneId -> zoneId.getOffset().getTotalSeconds())) - .collect(Collectors.toList()); + .toList(); antarcticZones.forEach(zdt -> System.out.printf("UTC%6s: %25s %7s%n", zdt.getOffset(), zdt.getZone(), diff --git a/src/main/java/refactoring/after/LoopsSortsAndIfs.java b/src/main/java/refactoring/after/LoopsSortsAndIfs.java index 4b63ff1..01fef00 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.Comparator; -import java.util.List; +import java.util.*; 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/sorting/Golfer.java b/src/main/java/sorting/Golfer.java index fdea0fe..0644f3c 100644 --- a/src/main/java/sorting/Golfer.java +++ b/src/main/java/sorting/Golfer.java @@ -1,5 +1,7 @@ package sorting; +import java.util.Objects; + public class Golfer implements Comparable { private String first; private String last; @@ -54,8 +56,8 @@ public boolean equals(Object o) { Golfer golfer = (Golfer) o; - if (first != null ? !first.equals(golfer.first) : golfer.first != null) return false; - return last != null ? last.equals(golfer.last) : golfer.last == null; + if (!Objects.equals(first, golfer.first)) return false; + return Objects.equals(last, golfer.last); }