IDEA-161257 Add Java-8 migration fix to use Map.computeIfAbsent where appropriate

This commit is contained in:
Tagir Valeev
2016-09-16 10:00:47 +07:00
parent d59ecfe632
commit 9b54b344af
13 changed files with 250 additions and 76 deletions
@@ -0,0 +1,11 @@
// "Replace with 'computeIfAbsent' method call" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map, String key, String value) {
List<String> list = map.computeIfAbsent(key, k -> new ArrayList<>());
list.add(value);
}
}
@@ -0,0 +1,13 @@
// "Replace with 'computeIfAbsent' method call" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
String k;
public void testMap(Map<String, List<String>> map, String value) {
List<String> list = map.computeIfAbsent(this.k, k1 -> new ArrayList<>());
list.add(value);
}
}
@@ -0,0 +1,15 @@
// "Replace with 'computeIfAbsent' method call" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map, String key, String value) {
List<String> list = map.get(key);
if(list == nul<caret>l) {
list = new ArrayList<>();
map.put(key, list);
}
list.add(value);
}
}
@@ -0,0 +1,17 @@
// "Replace with 'computeIfAbsent' method call" "false"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map, String key, String value) {
int size = 5;
if(map.isEmpty()) size = 10;
List<String> list = map.get(key);
if(list == <caret>null) {
list = new ArrayList<>(size);
map.put(key, list);
}
list.add(value);
}
}
@@ -0,0 +1,17 @@
// "Replace with 'computeIfAbsent' method call" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
String k;
public void testMap(Map<String, List<String>> map, String value) {
List<String> list = map.get(this.k);
if(list == nul<caret>l) {
list = new ArrayList<>();
map.put(this.k, list);
}
list.add(value);
}
}
@@ -6,8 +6,8 @@ public class Main {
public void testGetOrDefault(Map<String, String> map, String key) {
Integer num = 123;
System.out.println(num);
num = map.get(k<caret>ey);
if(num == null) num = 0;
num = map.get(key);
if(num == nu<caret>ll) num = 0;
System.out.println(num);
}
}
@@ -7,8 +7,8 @@ public class Main {
private String str;
public void testGetOrDefault(Map<String, String> map, String key, Main other) {
str = map.get(k<caret>ey);
if(str == null) {
str = map.get(key);
if(str == nul<caret>l) {
/*
block comment
*/
@@ -5,8 +5,8 @@ public class Main {
private String str;
public void testGetOrDefault(Map<String, String> map, String key, Main other) {
str = map.get(k<caret>ey);
if(other.str == null) {
str = map.get(key);
if(other.str ==<caret> null) {
// comment
str = "";
}
@@ -5,8 +5,8 @@ public class Main {
private String str;
public void testGetOrDefault(Map<String, String> map, String key, Main other) {
str = map.get(k<caret>ey);
if(str == null) {
str = map.get(key);
if(s<caret>tr == null) {
// comment
other.str = "";
}
@@ -4,8 +4,8 @@ import java.util.Map;
public class Main {
public void testGetOrDefault(Map<String, String> map, String key, Main other) {
String a = null, str = map.get(k<caret>ey);
if(str == null) {
String a = null, str = map.get(key);
if(str == nu<caret>ll) {
// comment
str = "";
}