zoukankan      html  css  js  c++  java
  • 为什么Hashtable ConcurrentHashmap不支持key或者value为null

      

      ConcurrentHashmap HashMapHashtable都是key-value存储结构,但他们有一个不同点是 ConcurrentHashmapHashtable不支持key或者valuenull,而HashMap是支持的。为什么会有这个区别?在设计上的目的是什么?

      ConcurrentHashmapHashtable都是支持并发的,这样会有一个问题,当你通过get(k)获取对应的value时,如果获取到的是null时,你无法判断,它是putk,v)的时候valuenull,还是这个key从来没有做过映射。HashMap是非并发的,可以通过contains(key)来做这个判断。而支持并发的Map在调用m.containskey)和m.get(key),m可能已经不同了。

    HashMap.class:

    // 此处计算key的hash值时,会判断是否为null,如果是,则返回0,即key为null的键值对
        // 的hash为0。因此一个hashmap对象只会存储一个key为null的键值对,因为它们的hash值都相同。
        static final int hash(Object key) {
            int h;
            return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
        }
        // 将键值对放入table中时,不会校验value是否为null。因此一个hashmap对象可以存储
        // 多个value为null的键值对
        public V put(K key, V value) {
            return putVal(hash(key), key, value, false, true);
        }
        final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                       boolean evict) {
            Node<K,V>[] tab; Node<K,V> p; int n, i;
            if ((tab = table) == null || (n = tab.length) == 0)
                n = (tab = resize()).length;
            if ((p = tab[i = (n - 1) & hash]) == null)
                tab[i] = newNode(hash, key, value, null);
            else {
                Node<K,V> e; K k;
                if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                    e = p;
                else if (p instanceof TreeNode)
                    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
                else {
                    for (int binCount = 0; ; ++binCount) {
                        if ((e = p.next) == null) {
                            p.next = newNode(hash, key, value, null);
                            if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                                treeifyBin(tab, hash);
                            break;
                        }
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            break;
                        p = e;
                    }
                }
                if (e != null) { // existing mapping for key
                    V oldValue = e.value;
                    if (!onlyIfAbsent || oldValue == null)
                        e.value = value;
                    afterNodeAccess(e);
                    return oldValue;
                }
            }
            ++modCount;
            if (++size > threshold)
                resize();
            afterNodeInsertion(evict);
            return null;
        }

    Hashtable.class:

    public synchronized V put(K key, V value) {
            // 确保value不为空。这句代码过滤掉了所有value为null的键值对。因此Hashtable不能
            // 存储value为null的键值对
            if (value == null) {
                throw new NullPointerException();
            }
            // 确保key在table数组中尚未存在。
            Entry<?,?> tab[] = table;
            int hash = key.hashCode(); //在此处计算key的hash值,如果此处key为null,则直接抛出空指针异常。
            int index = (hash & 0x7FFFFFFF) % tab.length;
            @SuppressWarnings("unchecked")
            Entry<K,V> entry = (Entry<K,V>)tab[index];
            for(; entry != null ; entry = entry.next) {
                if ((entry.hash == hash) && entry.key.equals(key)) {
                    V old = entry.value;
                    entry.value = value;
                    return old;
                }
            }
            addEntry(hash, key, value, index);
            return null;
    }
  • 相关阅读:
    Navicat Premium12以上版本多用户破解方法
    Linux并行gzip压缩工具pigz
    Windows Server 2019远程桌面服务配置和授权激活
    mysql删除大表
    KVM qcow2 磁盘在线扩容方法
    在jenkins中连接kubernetes集群
    CentOS 7部署 Ceph分布式存储架构
    (转)关于T(n) = kT(n/c) + f(n) 的时间复杂度
    算法中的思想(第0篇)
    (求通俗易懂的证法) 过n个有标志顶点的树的数目等于n^(n-2)
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/10928334.html
Copyright © 2011-2022 走看看