computeIfAbsent()
1、首先会判断map中是否有对应的Key;
2.1、如果没有对应的Key,则会创建一个满足Value类型的数据结构放到Value的位置中;
2.2、如果有对应的Key,则会操作该Key对应的Value.
public static void main(String[] args) {
HashMap<String, List<Integer>> map = new HashMap<>();
map.computeIfAbsent("hadoop", key -> new ArrayList<>()).add(1);
System.out.println(map);
map.computeIfAbsent("hadoop", key -> new ArrayList<>()).add(2);
System.out.println(map);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)