StreamToLoopInspection: support Map.forEach()

Fixes IDEA-190440 Convert usage of java.util.Map.forEach() to for (... entrySet()) loop
This commit is contained in:
Tagir Valeev
2018-04-19 12:12:40 +07:00
parent e8487f15df
commit c76d9aba62
6 changed files with 172 additions and 28 deletions
@@ -0,0 +1,34 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.Map;
import java.util.function.BiConsumer;
public class Main {
void test(Map<String, Integer> map) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String k = entry.getKey();
Integer v = entry.getValue();
if (k.isEmpty()) continue;
System.out.println("Key: " + k + "; value: " + v);
}
}
void test(Map<String, Integer> map, BiConsumer<String, Integer> consumer) {
int entry = 1;
for (Map.Entry<String, Integer> e : map.entrySet()) {
String key = e.getKey();
Integer value = e.getValue();
consumer.accept(key, value);
}
}
void test(Map<String, Integer> map, Map<String, Integer> otherMap) {
int entry = 1, e = 2, key = 3, value = 4;
for (Map.Entry<String, Integer> mapEntry : map.entrySet()) {
String k = mapEntry.getKey();
Integer v = mapEntry.getValue();
otherMap.putIfAbsent(k, v);
}
}
}