The traditional code works well like below:
Map<Integer, List<Integer>> map = new HashMap<>();
if (!map.containsKey(1)) {
map.put(1, new ArrayList<>());
}
map.get(1).add(2);
Now I'd like to try the magic of getOrDefault:
map.getOrDefault(1, new ArrayList<>()).add(2);
But if I use the above line, then map.get(1)
is null.
Why?