-
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.
Optimized code and improved readability in LazyStreams and LoopsSorts…
…AndIfs In LazyStreams.java, the redundant call to Logger class is removed which is not used in the rest of the file. This optimizes memory use. In the firstEvenDoubleDivBy3 function, sequence of function calls is rearranged to avoid unnecessary operations when the stream is parallelized. In LoopsSortsAndIfs.java, traditional for loops are replaced with Java 8 Streams to increase code readability and conciseness. The resulting code is more streamlined and efficient. The processing steps are visible in one line, enabling easier understanding and maintenance.
- Loading branch information
Showing
2 changed files
with
11 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,17 @@ | ||
package refactoring.after; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
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()); | ||
} | ||
} | ||
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); | ||
|
||
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); | ||
} | ||
} | ||
} |