紙上得來終覺淺,絕知此事要躬行。
爲了更貼近做者的實現意圖,以及JDK中每一個類的功能特色,決定從源碼的註釋中和實現來窺探其真諦。字斟句酌、查缺補漏;順帶提升英文閱讀能力;首先從HashMap入手:安全
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.
Hash table based implementation of the Map interface.
哈希表基於Map接口實現。數據結構
This implementation provides
all of the optional map operations, and permits
null values and the null key.
這個實現提供了全部可選擇的map操做,容許null的鍵和值。併發
The HashMap class is roughly equivalent to
Hashtable, except
that it is unsynchronized and permits nulls.
HashMap類大體上和Hashtable等價,除了它是非線程安全的和容許null鍵值。app
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.
這個類不保證map的順序;特別是它也不能保證順序會隨時間保持不變。ide
This implementation provides constant-time performance for the basic operations (get,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.
This implementation provides constant-time performance for the basic operations (get,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).
集合視圖的迭代須要時間與HashMap實例的容量(桶的數量)加上每一個桶的大小(鍵值對的數量)成比例。性能
Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
所以,若是看中迭代性能的話,不要設置初始容量太大或者負載因子過小,這點很重要的。ui
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.
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor.
HashMap實例有兩個參數會影響其性能:初始化容量和負載因子。this
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.
容量即hash表中桶的數量,那初始化容量就是hash表被建立時的容量。spa
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.
當哈希表中實體的數量超過負載因子和當前容量的乘積時,哈希表會被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.
As a general rule
, the default load factor (.75) offers a good tradeoff
between time and space costs.
通常來講,默認的負載因子0.75在時間和空間成本上提供了很好的權衡。
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).
更大的值減小了空間開銷可是增長了查找成本(會反應在HashMap類的大多數操做中,包括get和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.
在設置它的初始化容量時,應該考慮到預期的map中的條目數量和它的負載因子,來最小化rehash操做的數量。
If the initial capacity is greater than the maximum number of entries divided
by the load factor, no rehash operations will ever occur.
若是初始化容量大於條目最大數量除以負載因子,就不會發生rehash操做。
entry count < capacity * loadfactor 不會發生rehash (即capacity>count/loadfactor 不會發生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.
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.
若是有不少的鍵值對要存到hashmap的實例中,建立一個足夠大的容量的hashmap將會使得鍵值對的存儲比讓它根據須要自動作rehash以增加表更加有效。
Note that using many keys with the same hashCode is a sure way to slow down performance of any hash table.
注意,使用具備相同hashCode值的多個鍵確實會拖慢任何哈希表的性能。
To ameliorate impact
, when keys are Comparable, this class may use comparison
order among keys to help break ties
.
爲了減輕碰撞,當鍵有可比性時,這個類能夠經過鍵之間的比較順序來斷絕關係。
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.
If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method.
若是沒有這樣的對象存在,map應該經過Collections.synchronizedMap方法來封裝。(全部方法都加上synchronized)
This is best done at creation time, to prevent accidental
unsynchronized access to the map.
最好是在建立的時候完成,來防止意外的非線程安全的訪問map。
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.
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.
這個類的全部集合視圖方法的迭代器的返回都遵循fail-fast策略: 若是map在建立完迭代器以後的任什麼時候機結構發生改變,除了經過迭代器本身的remove方法外,迭代器將會拋出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.
所以,當併發修改的狀況下,迭代器會快速乾淨的失敗而不是在未來某個不肯定的時間冒着任意的、不肯定行爲的風險。
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.
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行爲不能被保證,一般來講,在非線程安全的併發修改存在的狀況下,不可能作任何硬性的保證。
Fail-fast iterators throw ConcurrentModificationException on a best-effort basis
.
迭代器的fail-fast機制拋出ConcurrentModificationException是最佳的處理方式。
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.
所以,編寫依賴於這個異常的程序來保證其正確性是錯誤的作法:迭代器的fail-fast行爲僅僅應該用來探測bugs。
核心詞彙:provide:提供
permit:容許
roughly:大致上、大體上
equivalent to:等於
except除了
guarantee:保證、擔保
constant: 常數,常量、不變的事物
assume:假定、認爲、假設
disperses:分散
proportional:成比例的
capacity:容量
measure:度量、測量
exceed:超過
internal:內部的
structure:結構
approximately:大約
tradeoff:折衷
overhead:開銷、費用
expected:指望的、預期的
taken into account:考慮到
divide:分、劃分、除以
sufficiently:足夠地、充分地
ameliorate:改善、改良、減輕
impact:碰撞、影響
comparison:比較
ties:結、關係
multiple:併發的、多重的
structurally:在結構上的
externally:在..外部、在..外面
typically:一般、典型地
accomplish:完成
naturally:天然地、合理地、固然、不用說
encapsulate:封裝、概述
prevent:預防、阻止
accidental:意外的、偶然的
arbitrary:隨意的、任性的、爲所欲爲的
non-deterministic:非肯定的
undetermined:未肯定的
generally speaking:一般來講
presence:出席
best-effort basis:盡力而爲、盡最大努力
correctness:正確性
detect:查明、發現、洞察