CollectMigration: support toUnmodifiableMap

Fixes IDEA-187213 Stream API migration: support Java 10 toUnmodifiableList/Set/Map collections
This commit is contained in:
Tagir Valeev
2018-04-17 15:05:45 +07:00
parent 000f124c33
commit a1a4663b73
3 changed files with 57 additions and 6 deletions
@@ -20,4 +20,17 @@ class Test {
// toUnmodifiableSet will not preserve order; not suggested here
return Collections.unmodifiableSet(result);
}
Map<String, Integer> map(List<String> input) {
return input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toUnmodifiableMap(s -> s, s -> s.length(), (a, b) -> b));
}
Map<String, Integer> map1(List<String> input) {
Map<String, Integer> result = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toMap(s -> s, String::length, (a, b) -> b, TreeMap::new));
return Collections.unmodifiableMap(result);
}
Map<String, Integer> map2(int[] input) {
return Arrays.stream(input).filter(s -> s > 0).boxed().collect(Collectors.toUnmodifiableMap(s -> String.valueOf(s), s -> s, (a, b) -> b));
}
}
@@ -47,4 +47,34 @@ class Test {
// toUnmodifiableSet will not preserve order; not suggested here
return Collections.unmodifiableSet(result);
}
Map<String, Integer> map(List<String> input) {
Map<String, Integer> result = new HashMap<>(100);
for (String s : input) {
if (!s.isEmpty()) {
result.put(s, s.length());
}
}
return Collections.unmodifiableMap(result);
}
Map<String, Integer> map1(List<String> input) {
Map<String, Integer> result = new TreeMap<>();
for (String s : input) {
if (!s.isEmpty()) {
result.put(s, s.length());
}
}
return Collections.unmodifiableMap(result);
}
Map<String, Integer> map2(int[] input) {
Map<String, Integer> result = new HashMap<>();
for (int s : input) {
if (s > 0) {
result.put(String.valueOf(s), s);
}
}
return Collections.unmodifiableMap(result);
}
}