首页 > 其他 > 详细

ConcurrentHashMap putIfAbsent和的put区别

时间:2016-12-14 02:38:46      阅读:688      评论:0      收藏:0      [点我收藏+]

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
222bbb

put    

    与之hashMap相同,当key存在时,put同样的key将被覆盖

本文出自 “幕后黑手” 博客,请务必保留此出处http://11942699.blog.51cto.com/11932699/1882402

ConcurrentHashMap putIfAbsent和的put区别

原文:http://11942699.blog.51cto.com/11932699/1882402

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!