一文读懂HashMap和HashTable的区别以及常见面试题(31)

\n

1、HashTable的扩容操作 , 在put方法中 , 如果需要向table[
中添加Entry元素 , 会首先进行容量校验 , 如果容量已经达到了阀值 , HashTable就会进行扩容处理rehash() , 如下:

\n

protected void rehash() {
       int oldCapacity = table.length;        //元素
       Entry<KV>[
oldMap = table;        //新容量=旧容量 * 2 + 1
       int newCapacity = (oldCapacity << 1) + 1;        if (newCapacity - MAX_ARRAY_SIZE > 0) {            if (oldCapacity == MAX_ARRAY_SIZE)                return;            newCapacity = MAX_ARRAY_SIZE;
               //新建一个size = newCapacity 的HashTable
       Entry<KV>[
newMap = new Entry[

推荐阅读