摘要:為了更貼近作者的實現意圖,以及中每個類的功能特點,決定從源碼的注釋中和實現來窺探其真諦。注意,迭代器本身的行為不能被保證,通常來說,在非線程安全的并發修改存在的情況下,不可能做任何硬性的保證。迭代器的機制拋出是最佳的處理方式。
紙上得來終覺淺,絕知此事要躬行。
為了更貼近作者的實現意圖,以及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鍵值。
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的順序;特別是它也不能保證順序會隨時間保持不變。
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.
因此,如果看中迭代性能的話,不要設置初始容量太大或者負載因子太小,這點很重要的。
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實例有兩個參數會影響其性能:初始化容量和負載因子。
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表被創建時的容量。
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:查明、發現、洞察
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/76845.html
摘要:為了避免一篇文章的篇幅過長,于是一些比較大的主題就都分成幾篇來講了,這篇文章是筆者所有文章的目錄,將會持續更新,以給大家一個查看系列文章的入口。 前言 大家好,筆者是今年才開始寫博客的,寫作的初衷主要是想記錄和分享自己的學習經歷。因為寫作的時候發現,為了弄懂一個知識,不得不先去了解另外一些知識,這樣以來,為了說明一個問題,就要把一系列知識都了解一遍,寫出來的文章就特別長。 為了避免一篇...
摘要:在二叉查找樹強制一般要求以外,對于任何有效的紅黑樹增加了如下的額外要求節點是紅色或黑色。紅黑樹有哪些應用場景內核和系統調用實現中使用的完全公平調度程序使用紅黑樹。 前言 這篇文章是記錄自己分析 Java 8 的 HashMap 源碼時遇到的疑問和總結,在分析的過程中筆者把遇到的問題都記錄下來,然后逐一擊破,如果有錯誤的地方,希望讀者可以指正,筆者感激不盡。 疑問與解答 什么是 initia...
摘要:在二叉查找樹強制一般要求以外,對于任何有效的紅黑樹增加了如下的額外要求節點是紅色或黑色。紅黑樹有哪些應用場景內核和系統調用實現中使用的完全公平調度程序使用紅黑樹。 前言 這篇文章是記錄自己分析 Java 8 的 HashMap 源碼時遇到的疑問和總結,在分析的過程中筆者把遇到的問題都記錄下來,然后逐一擊破,如果有錯誤的地方,希望讀者可以指正,筆者感激不盡。 疑問與解答 什么是 initia...
閱讀 1407·2021-11-24 10:20
閱讀 3649·2021-11-24 09:38
閱讀 2294·2021-09-27 13:37
閱讀 2196·2021-09-22 15:25
閱讀 2270·2021-09-01 18:33
閱讀 3488·2019-08-30 15:55
閱讀 1783·2019-08-30 15:54
閱讀 2081·2019-08-30 12:50