computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) 如果map里面不存在key,则执行mappingFunction
computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) 如果map里面存在key,则执行remappingFunction
compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) 无论map里面是不是有key,都执行remappingFunction
注意computeIfAbsent的第二个参数是k,computeIfPresent和compute第二个参数都是(k,v)
public static void main(String[] args) {
HashMap<String, List<String>> map = new HashMap();
// key不存在的时候,执行后面的方法
List<String> list1 = map.computeIfAbsent("list1", k -> new ArrayList<>());
list1.add("addList1");
map.put("absentExist", new ArrayList<>());
List<String> absentExist = map.computeIfAbsent("absentExist", k -