HashMap与ArrayList 的扩容问题

HashMap与ArrayList 的扩容问题


1. HashMap扩容

  HashMap 的实例有两个参数影响其性能:初始容量 和加载因子。
容量是哈希表中桶的数量,初始容量只是哈希表在创建时的容量。
加载因子 是哈希表在其容量自动增加之前可以达到多满的一种尺度。
当哈希表中的条目数超出了加载因子与当前容量的乘积时,则要对该哈希表进行 rehash 操作(即重建内部数据结构),从而哈希表将具有大约两倍的桶数。

  通常,默认加载因子 (.75) 在时间和空间成本上寻求一种折衷。
加载因子过高虽然减少了空间开销,但同时也增加了查询成本(在大多数 HashMap 类的操作中,包括 get 和 put 操作,都反映了这一点)。

  在设置初始容量时应该考虑到映射中所需的条目数及其加载因子,以便最大限度地减少 rehash 操作次数。
如果初始容量大于最大条目数除以加载因子,则不会发生 rehash 操作。

  如果很多映射关系要存储在 HashMap 实例中,则相对于按需执行自动的 rehash 操作以增大表的容量来说,使用足够大的初始容量创建它将使得映射关系能更有效地存储。

HashMap()
   构造一个具有默认初始容量 (16) 和默认加载因子 (0.75) 的空 HashMap。

如果能明确数据的个数:初始容量就比较好办,如果无法明确,那么要考虑他的每次增长是*2
源码如下:

/** 
    * Adds a new entry with the specified key, value and hash code to 
    * the specified bucket.  It is the responsibility of this 
    * method to resize the table if appropriate. 
    * 
    * Subclass overrides this to alter the behavior of put method. 
    */  
   void addEntry(int hash, K key, V value, int bucketIndex) {  
Entry<K,V> e = table[bucketIndex];  
       table[bucketIndex] = new Entry<K,V>(hash, key, value, e);  
       if (size++ >= threshold)  
           resize(2 * table.length);  
   }  
   /** 
    * Rehashes the contents of this map into a new array with a 
    * larger capacity.  This method is called automatically when the 
    * number of keys in this map reaches its threshold. 
    * 
    * If current capacity is MAXIMUM_CAPACITY, this method does not 
    * resize the map, but sets threshold to Integer.MAX_VALUE. 
    * This has the effect of preventing future calls. 
    * 
    * @param newCapacity the new capacity, MUST be a power of two; 
    *  must be greater than current capacity unless current 
    *  capacity is MAXIMUM_CAPACITY (in which case value 
    *        is irrelevant). 
    */  
   void resize(int newCapacity) {  
       Entry[] oldTable = table;  
       int oldCapacity = oldTable.length;  
       if (oldCapacity == MAXIMUM_CAPACITY) {  
           threshold = Integer.MAX_VALUE;  
           return;  
       }  

       Entry[] newTable = new Entry[newCapacity];  
       transfer(newTable);  
       table = newTable;  
       threshold = (int)(newCapacity * loadFactor);  
   }  

欲知详情请点击讲得很好

2. ArrayList扩容

源码如下:

public void ensureCapacity(int minCapacity) {
 modCount++;
 int oldCapacity = elementData.length;
 if (minCapacity > oldCapacity) {
     Object oldData[] = elementData;
     int newCapacity = (oldCapacity * 3)/2 + 1;
     if (newCapacity < minCapacity) newCapacity = minCapacity;
     // minCapacity is usually close to size, so this is a win:
     elementData = Arrays.copyOf(elementData, newCapacity);
 }
}

  所以说ArrayList扩容是1.5倍+1

事实上元素数据并非直接存在ArrayList对象里,而是存在其进一步引用的数组里。即便有final引用指向一个对象,也不妨碍该对象内部的字段得到新的赋值。具体到ArrayList,即便有final字段指向一个ArrayList,它里面的elementData字段还是可以改变指向,在扩容、缩容时指向别的新的数组对象。

实际上java.util.ArrayList是个固定大小的对象,自身并不直接存储元素内容,而是持有一个引用指向一个数组,真正负责存储元素内容的正是这个数组。
当需要扩容时,扩容的是这个数组,而不是ArrayList对象自身。

同上附上大神详解链接



              整理知识何乐而不为~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值