本期關鍵詞:
equivalent ['kwv()l()nt] adj. 等價的,相等的;guarantee [gr()n'ti] vt. 保證;擔保constant ['knst()nt] adj. 不變的;恆定的; n. [數] 常數;disperse [d'sps] vt. 分散;使散開;bucket ['bkt] n. 桶,水桶;一桶的量Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
中文:HashMap基於Map接口實現,提供了所有可選的map操作,允許空鍵和空值。(除了非線程安全、允許空鍵和空值,HashMap和HashTable基本類似)。HashMap不保證鍵值對的順序(即存儲順序跟put順序不一致),也不保證這個順序不會發生改變(當rehash時,可能會發生變化)。
This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
中文:假使Hash函數能夠將元素均勻的分布在不同的桶中,HashMap對於基本操作(get和put)可以提供穩定的性能( constant-time performance)。遍曆元素所需時間取決於HashMap實例的容量(capacity,桶的數量)和大小(size,鍵值對的數量)。如果對迭代性能要求較高,那麼不要將初始容量設得過高或者負載因子設得過低(初始容量過高或負載因子過低,都會導致較多的空桶)。
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
中文:影響HashMap實例性能的參數有兩個:初始容量和負載因子( initial capacity and load factor)。容量是指哈希表中桶(bucket)的數量,初始容量即哈希表在創建時的桶的數量,默認為16。負載因子是衡量哈希表在擴容前其容量能達到多滿的一個尺度,當哈希表中的條目(entries )數超過了負載因子與當前容量的乘積時,就會進行rehash操作(重建哈希表內部數據結構),哈希表中桶的數量會擴展為原來的兩倍。
As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.
中文:HashMap默認負載因子為0.75,是對時間和空間開銷的權衡。較高的負載因子可以減少空間開銷,但卻會增加查找成本(反映在HashMap的大多操作上,包括get和put等)。在設置初始容量時,要考慮哈希表預計存儲的條目數以及負載因子大小,儘量減少rehash操作的次數。如果初始容量大於預計存放條目數除以負載因子的商(預計條目數小於初始容量與負載因子的乘積),則永遠不會發生 rehash 操作。
If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table. Note that using many keys with the same hashCode() is a sure way to slow down performance of any hash table. To ameliorate impact, when keys are Comparable, this class may use comparison order among keys to help break ties.
中文:如果提前知道要存儲很多條目,在創建HahMap實例時就要設置足夠大的初始容量,這樣可以省去自動擴容帶來的性能開銷。 注意,如果很多鍵的哈希值相同, 會降低哈希表的性能,這些鍵可以實現Comparable,這樣這個類(HashMap)就能在這些鍵之間使用比較來減少這種影響。(To ameliorate impact, when keys are java.lang.Comparable, this class may use comparison order among keys to help break ties.)
Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
Map m = Collections.synchronizedMap(new HashMap(...));中文:HashMap的實現不是同步的,如果多個線程並發地訪問一個哈希表,且至少有一個線程修改哈希表結構,那麼必須在該HashMap的外部進行同步(結構改變是指添加或刪除鍵值對的操作,僅僅改變鍵值對的值不是結構性修改)。通常,可以對包含這個哈希表的對象進行同步來實現線程安全(This is typically accomplished by synchronizing on some object that naturally encapsulates the map. )。如果沒有這樣的對象,應該用 Collections.synchronizedMap來包裝HashMap。為了避免意外的非同步訪問,最好在創建的時候就對HashMap進行包裝,示例如下。
Map m = Collections.synchronizedMap(new HashMap(…));
The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
中文:HashMap遍曆元素採用快速失敗(fail-fast:)機制。在迭代器創建之後,除了迭代器自己的remove方法,任何對哈希表的結構性修改,迭代器都會拋出並發修改異常(ConcurrentModificationException)。這樣,對於並發修改,迭代器簡潔明了的拋出異常,避免了未來某個不確定時間某個不確定行為帶來的風險。
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
中文:需要注意的是,迭代器並不能保證一定快速失敗,一般來說,對於非同步的並發修改,迭代器不可能做出堅決的保證一定會快速失敗。快速失敗迭代器只是盡最大努力拋出 ConcurrentModificationException。因此,編寫依賴於此異常的程序的做法是錯誤的,正確的做法應該是:迭代器的快速失敗行為僅用於檢測程序Bug。
This class is a member of the Java Collections Framework.
中文:HashMap類是Java集合框架中的一員。