HashTable 分析

HashTable经常拿来跟HashMap比较,今天来看看HashTable的源码。从无参构造方法出发。

//初始的容量为11,装载因子为0.75。跟HashMap一样。初始化时平平无奇。
   public Hashtable(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);

        if (initialCapacity==0)
            initialCapacity = 1;
        this.loadFactor = loadFactor;
        //HashTableEntry属于单向链表。
        table = new HashtableEntry<?,?>[initialCapacity];
        // Android-changed: Ignore loadFactor when calculating threshold from initialCapacity
        // threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
        // threshold 为阀值,代表超过这个值就需要扩容。
        threshold = (int)Math.min(initialCapacity, MAX_ARRAY_SIZE + 1);
    }

现在我们开始分析主要逻辑,put和get。同步方法。

  public synchronized V put(K key, V value) {
        // Make sure the value is not null
        if (value == null) {
            throw new NullPointerException();
        }

        // Makes sure the key is not already in the hashtable.
        HashtableEntry<?,?> tab[] = table;
        //直接求出key的hashcode。
        int hash = key.hashCode();
        //为啥要把hash值和0x7FFFFFFF做一次按位与操作呢,
		// 主要是为了保证得到的index的第一位为0,也就是为了得到一个正数。
		// 因为有符号数第一位0代表正数,1代表负数。
		//这个方法体现了hash思想,将一个key跟一个小于tab.length进行了关联。
        int index = (hash & 0x7FFFFFFF) % tab.length;
        @SuppressWarnings("unchecked")
        HashtableEntry<K,V> entry = (HashtableEntry<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;
            }
        }
       //如果entry为null,那么直接加入了。
        addEntry(hash, key, value, index);
        return null;
    }

分析下addEntry 方法。

  private void addEntry(int hash, K key, V value, int index) {
        modCount++;

        HashtableEntry<?,?> tab[] = table;
        //如果达到阀值了。那么重新扩容,一会来分析。
        if (count >= threshold) {
            // Rehash the table if the threshold is exceeded
            rehash();

            tab = table;
            hash = key.hashCode();
            index = (hash & 0x7FFFFFFF) % tab.length;
        }

        // Creates the new entry.
        @SuppressWarnings("unchecked")
        HashtableEntry<K,V> e = (HashtableEntry<K,V>) tab[index];
        tab[index] = new HashtableEntry<>(hash, key, value, e);
        count++;
    }
 protected void rehash() {
        int oldCapacity = table.length;
        HashtableEntry<?,?>[] oldMap = table;

        // overflow-conscious code
        //扩容  原容量*2+1
        int newCapacity = (oldCapacity << 1) + 1;
        if (newCapacity - MAX_ARRAY_SIZE > 0) {
            if (oldCapacity == MAX_ARRAY_SIZE)
                // Keep running with MAX_ARRAY_SIZE buckets
                return;
            newCapacity = MAX_ARRAY_SIZE;
        }
        HashtableEntry<?,?>[] newMap = new HashtableEntry<?,?>[newCapacity];

        modCount++;
        //重新计算阀值。
        threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
        table = newMap;
        //老的table的赋值到新的table。 
        for (int i = oldCapacity ; i-- > 0 ;) {
            for (HashtableEntry<K,V> old = (HashtableEntry<K,V>)oldMap[i] ; old != null ; ) {
                HashtableEntry<K,V> e = old;
                old = old.next;

                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
                e.next = (HashtableEntry<K,V>)newMap[index];
                newMap[index] = e;
            }
        }
    }

可以看到put方法中如果index碰撞了,解决思想就是直接覆盖了。但是HashMap不会,它会把对象插入对对应数组的头结点。还有一个扩容大小不一样。相比较而言,HashMap要复杂的很多了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值