Map.get()
which could be replaced with getOrDefault()
, computeIfAbsent()
or
putIfAbsent()
.
String val = map.containsKey(key) ? map.get(key) : "none";
List<String> list = map.get(key); if (list == null) { list = new ArrayList<>(); map.put(key, list); }
String val = map.get(key); if (val == null) map.put(key, newVal);
Integer val = map.get(key); if (val == null) map.put(key, 1); else map.put(key, val + 1);
Note that replacement with computeIfAbsent()
or merge()
may work incorrectly for some Map
implementations if the code extracted to lambda expression modifies the same Map
. By default,
warning is not issued if this code may have side effects. If desired, use the last checkbox to issue warning always.
This inspection only reports if the project or module is configured to use a language level of 8 or higher.
New in 2016.3