// 方法定義
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
...
}
// java8之前。從map中根據key獲取value操作可能會有下面的操作
Object key = map.get("key");
if (key == null) {
key = new Object();
map.put("key", key);
}
// java8之后。上面的操作可以簡化為一行,若key對應的value為空,會將第二個參數的返回值存入並返回
List<String> list2 = map.computeIfAbsent("key", k -> new ArrayList<String>());
list2.add("123");
Object key2 = map.computeIfAbsent("key", k -> new Object());
key2.toString();