-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code to use modern Java features
Code has been updated across multiple files to use more modern Java features. These changes include modifying loops and if statements to use streams, replacing a ArrayList with List.of() for immutability, introducing new record PersonRecord, and updating error logging in LazyStreams. This code cleanup improves readability and makes better use of Java's capabilities.
- Loading branch information
Showing
6 changed files
with
59 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package lambdas; | ||
|
||
// Records: | ||
// - are immutable data holders | ||
// - automatically generate equals(), hashCode(), and toString() | ||
// - "getters" match the property names, as in "name()" | ||
// - primary constructor is declared before the braces {} | ||
public record PersonRecord(String name) { | ||
|
||
public PersonRecord(String first, String last) { | ||
this(first + " " + last); | ||
} | ||
|
||
// Copy constructor | ||
public PersonRecord(PersonRecord other) { | ||
this(other.name); | ||
} | ||
|
||
public PersonRecord(String... names) { | ||
this(String.join(" ", names)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |