Skip to content

Commit

Permalink
Refactor code with Stream API and modernize object comparisons.
Browse files Browse the repository at this point in the history
Replaced loops and collections manipulations with Stream API in `LoopsSortsAndIfs` for better readability and performance. Updated `AntarcticaTimeZones` to use `toList()` instead of `collect`. Simplified `Golfer` equals method using `Objects.equals`.
  • Loading branch information
kousen committed Aug 14, 2024
1 parent a652936 commit 3e29486
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 26 deletions.
3 changes: 1 addition & 2 deletions src/main/java/datetime/AntarcticaTimeZones.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,7 +17,7 @@ public static void main(String[] args) {
.map(now::atZone) // Stream<ZonedDateTime>
.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(),
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.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<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);
}
}
6 changes: 4 additions & 2 deletions src/main/java/sorting/Golfer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sorting;

import java.util.Objects;

public class Golfer implements Comparable<Golfer> {
private String first;
private String last;
Expand Down Expand Up @@ -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);

}

Expand Down

0 comments on commit 3e29486

Please sign in to comment.