StreamToLoop inspection: simplify toMap mergers (a,b)->a (use putIfAbsent) and (a,b)->b (use put)

This commit is contained in:
Tagir Valeev
2016-12-06 12:23:06 +07:00
parent f3d41d8a3c
commit f0c8802a27
4 changed files with 60 additions and 1 deletions
@@ -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", "")));
}
}
@@ -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;
@@ -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", "")));
}
}