putIfAbsent:
当key不存在的时候调用put方法将key存入进map
当key存在的时候相当于return map.get(key)
public static void main(String[] args) { ConcurrentHashMap map = new ConcurrentHashMap(); Object o = map.putIfAbsent("aaa", "bbb"); System.out.println("111"+o); Object o1 = map.putIfAbsent("aaa", "bbb"); System.out.println("222"+o1); }
输出
111null
222bbbput
与之hashMap相同,当key存在时,put同样的key将被覆盖