getOrDefault() does not mutate the map, so you will not find the value on the map if you subsequently checked its contents, for example.
HashMap<String, String> map = new HashMap<>();
map.getOrDefault("something", "default");
assertTrue(map.isEmpty());
putIfAbsent() mutates the map, so you will find the values on the map if you later check its contents, for example
HashMap<String, String> map = new HashMap<>();
map.putIfAbsent("something", "default");
assertFalse(map.isEmpty());
You must choose the one that suits you.
source
share