问八:HashMap、Hashtable、ConcurrentHashMap三者的区别是什么?

HashMap线程不安全,数组+链表+红黑树

Hashtable线程安全,锁住整个对象,数组+链表

ConccurentHashMap线程安全,CAS+同步锁,数组+链表+红黑树

HashMap的key/value均可以为null,而其他两个类不支持(特别注意空字符串和null的区别),也就是说HashMap中getKey取到为null的时候,不一定是代表在hash表中没有当前值,也有可能是存了null值[它是通过指向一个空字符?来实现的]

public class Demo {

     public static void main(String[] args) {

          HashMap<String, String> map = new HashMap<>();

          map.put("test", null);

          System.out.println(map.get("test") ==  map.get("996"));

     }

}

true

猜你喜欢

转载自blog.csdn.net/qq_18657175/article/details/89374031