mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-08-26 06:11:42 +07:00
StreamToLoop inspection: simplify toMap mergers (a,b)->a (use putIfAbsent) and (a,b)->b (use put)
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static TreeMap<Integer, String> test(List<String> strings) {
|
||||
TreeMap<Integer, String> map = new TreeMap<>();
|
||||
for (String s1 : strings) {
|
||||
if (!s1.isEmpty()) {
|
||||
map.put(s1.length(), s1.trim());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -8,7 +8,7 @@ public class Main {
|
||||
TreeMap<Integer, String> map = new TreeMap<>();
|
||||
for (String s1 : strings) {
|
||||
if (!s1.isEmpty()) {
|
||||
map.merge(s1.length(), s1, (s, string) -> s);
|
||||
map.putIfAbsent(s1.length(), s1);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static TreeMap<Integer, String> test(List<String> strings) {
|
||||
return strings.stream().filter(s -> !s.isEmpty())
|
||||
.col<caret>lect(Collectors.toMap(String::length, String::trim, (s, string) -> string, TreeMap::new));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user