2013-02-08 39 views

回答

4

不。表格保留其大小。所有的元素都设置为null

public void clear() { 
    modCount++; 
    Entry[] tab = table; 
    for (int i = 0; i < tab.length; i++) 
     tab[i] = null; 
    size = 0; 
} 
2

这是一个实现细节,我不知道what API you're reading,说任何有关1M提出或内部哈希表。

就让我们来看看一个实现:

620  /** 
    621  * Removes all of the mappings from this map. 
    622  * The map will be empty after this call returns. 
    623  */ 
    624  public void clear() { 
    625   modCount++; 
    626   Entry[] tab = table; 
    627   for (int i = 0; i < tab.length; i++) 
    628    tab[i] = null; 
    629   size = 0; 
    630  } 

http://www.docjar.com/html/api/java/util/HashMap.java.html#621

所以OpenJDK 7的实现不恢复原来的尺寸。

相关问题