摘要:為了更貼近作者的實(shí)現(xiàn)意圖,以及中每個(gè)類的功能特點(diǎn),決定從源碼的注釋中和實(shí)現(xiàn)來窺探其真諦。注意,迭代器本身的行為不能被保證,通常來說,在非線程安全的并發(fā)修改存在的情況下,不可能做任何硬性的保證。迭代器的機(jī)制拋出是最佳的處理方式。
紙上得來終覺淺,絕知此事要躬行。
為了更貼近作者的實(shí)現(xiàn)意圖,以及JDK中每個(gè)類的功能特點(diǎn),決定從源碼的注釋中和實(shí)現(xiàn)來窺探其真諦。字斟句酌、查缺補(bǔ)漏;順帶提高英文閱讀能力;首先從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接口實(shí)現(xiàn)。
This implementation provides all of the optional map operations, and permits null values and the null key.
這個(gè)實(shí)現(xiàn)提供了所有可選擇的map操作,允許null的鍵和值。
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
HashMap類大致上和Hashtable等價(jià),除了它是非線程安全的和允許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.
這個(gè)類不保證map的順序;特別是它也不能保證順序會(huì)隨時(shí)間保持不變。
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.
假定哈希函數(shù)將元素適當(dāng)?shù)姆稚⒃诟鱾€(gè)桶中,這個(gè)實(shí)現(xiàn)為基本的操作(讀、寫)提供了恒定時(shí)間的性能。
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).
集合視圖的迭代需要時(shí)間與HashMap實(shí)例的容量(桶的數(shù)量)加上每個(gè)桶的大小(鍵值對(duì)的數(shù)量)成比例。
Thus, it"s very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
因此,如果看中迭代性能的話,不要設(shè)置初始容量太大或者負(fù)載因子太小,這點(diǎn)很重要的。
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實(shí)例有兩個(gè)參數(shù)會(huì)影響其性能:初始化容量和負(fù)載因子。
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表中桶的數(shù)量,那初始化容量就是hash表被創(chuàng)建時(shí)的容量。
The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
負(fù)載因子是在哈希表容量自動(dòng)增長(zhǎng)之前,哈希表所允許達(dá)到的最大容量的度量。
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.
當(dāng)哈希表中實(shí)體的數(shù)量超過負(fù)載因子和當(dāng)前容量的乘積時(shí),哈希表會(huì)被rehash(即內(nèi)部數(shù)據(jù)結(jié)構(gòu)重建)這樣哈希表的桶數(shù)量大約是原來的兩倍。
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.
一般來說,默認(rèn)的負(fù)載因子0.75在時(shí)間和空間成本上提供了很好的權(quán)衡。
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).
更大的值減少了空間開銷但是增加了查找成本(會(huì)反應(yīng)在HashMap類的大多數(shù)操作中,包括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.
在設(shè)置它的初始化容量時(shí),應(yīng)該考慮到預(yù)期的map中的條目數(shù)量和它的負(fù)載因子,來最小化rehash操作的數(shù)量。
If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.
如果初始化容量大于條目最大數(shù)量除以負(fù)載因子,就不會(huì)發(fā)生rehash操作。
entry count < capacity * loadfactor 不會(huì)發(fā)生rehash (即capacity>count/loadfactor 不會(huì)發(fā)生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.
如果有很多的鍵值對(duì)要存到hashmap的實(shí)例中,創(chuàng)建一個(gè)足夠大的容量的hashmap將會(huì)使得鍵值對(duì)的存儲(chǔ)比讓它根據(jù)需要自動(dòng)做rehash以增長(zhǎng)表更加有效。
Note that using many keys with the same hashCode is a sure way to slow down performance of any hash table.
注意,使用具有相同hashCode值的多個(gè)鍵確實(shí)會(huì)拖慢任何哈希表的性能。
To ameliorate impact, when keys are Comparable, this class may use comparison order among keys to help break ties.
為了減輕碰撞,當(dāng)鍵有可比性時(shí),這個(gè)類可以通過鍵之間的比較順序來斷絕關(guān)系。
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.
如果沒有這樣的對(duì)象存在,map應(yīng)該通過Collections.synchronizedMap方法來封裝。(所有方法都加上synchronized)
This is best done at creation time, to prevent accidental unsynchronized access to the map.
最好是在創(chuàng)建的時(shí)候完成,來防止意外的非線程安全的訪問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.
這個(gè)類的所有集合視圖方法的迭代器的返回都遵循fail-fast策略: 如果map在創(chuàng)建完迭代器之后的任何時(shí)機(jī)結(jié)構(gòu)發(fā)生改變,除了通過迭代器自己的remove方法外,迭代器將會(huì)拋出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.
因此,當(dāng)并發(fā)修改的情況下,迭代器會(huì)快速干凈的失敗而不是在將來某個(gè)不確定的時(shí)間冒著任意的、不確定行為的風(fēng)險(xiǎn)。
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行為不能被保證,通常來說,在非線程安全的并發(fā)修改存在的情況下,不可能做任何硬性的保證。
Fail-fast iterators throw ConcurrentModificationException on a best-effort basis.
迭代器的fail-fast機(jī)制拋出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.
因此,編寫依賴于這個(gè)異常的程序來保證其正確性是錯(cuò)誤的做法:迭代器的fail-fast行為僅僅應(yīng)該用來探測(cè)bugs。
核心詞匯:
provide:提供
permit:允許
roughly:大體上、大致上
equivalent to:等于
except除了
guarantee:保證、擔(dān)保
constant: 常數(shù),常量、不變的事物
assume:假定、認(rèn)為、假設(shè)
disperses:分散
proportional:成比例的
capacity:容量
measure:度量、測(cè)量
exceed:超過
internal:內(nèi)部的
structure:結(jié)構(gòu)
approximately:大約
tradeoff:折衷
overhead:開銷、費(fèi)用
expected:期望的、預(yù)期的
taken into account:考慮到
divide:分、劃分、除以
sufficiently:足夠地、充分地
ameliorate:改善、改良、減輕
impact:碰撞、影響
comparison:比較
ties:結(jié)、關(guān)系
multiple:并發(fā)的、多重的
structurally:在結(jié)構(gòu)上的
externally:在..外部、在..外面
typically:通常、典型地
accomplish:完成
naturally:自然地、合理地、當(dāng)然、不用說
encapsulate:封裝、概述
prevent:預(yù)防、阻止
accidental:意外的、偶然的
arbitrary:隨意的、任性的、隨心所欲的
non-deterministic:非確定的
undetermined:未確定的
generally speaking:通常來說
presence:出席
best-effort basis:盡力而為、盡最大努力
correctness:正確性
detect:查明、發(fā)現(xiàn)、洞察
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/76845.html
摘要:為了避免一篇文章的篇幅過長(zhǎng),于是一些比較大的主題就都分成幾篇來講了,這篇文章是筆者所有文章的目錄,將會(huì)持續(xù)更新,以給大家一個(gè)查看系列文章的入口。 前言 大家好,筆者是今年才開始寫博客的,寫作的初衷主要是想記錄和分享自己的學(xué)習(xí)經(jīng)歷。因?yàn)閷懽鞯臅r(shí)候發(fā)現(xiàn),為了弄懂一個(gè)知識(shí),不得不先去了解另外一些知識(shí),這樣以來,為了說明一個(gè)問題,就要把一系列知識(shí)都了解一遍,寫出來的文章就特別長(zhǎng)。 為了避免一篇...
摘要:為了避免一篇文章的篇幅過長(zhǎng),于是一些比較大的主題就都分成幾篇來講了,這篇文章是筆者所有文章的目錄,將會(huì)持續(xù)更新,以給大家一個(gè)查看系列文章的入口。 前言 大家好,筆者是今年才開始寫博客的,寫作的初衷主要是想記錄和分享自己的學(xué)習(xí)經(jīng)歷。因?yàn)閷懽鞯臅r(shí)候發(fā)現(xiàn),為了弄懂一個(gè)知識(shí),不得不先去了解另外一些知識(shí),這樣以來,為了說明一個(gè)問題,就要把一系列知識(shí)都了解一遍,寫出來的文章就特別長(zhǎng)。 為了避免一篇...
摘要:在二叉查找樹強(qiáng)制一般要求以外,對(duì)于任何有效的紅黑樹增加了如下的額外要求節(jié)點(diǎn)是紅色或黑色。紅黑樹有哪些應(yīng)用場(chǎng)景內(nèi)核和系統(tǒng)調(diào)用實(shí)現(xiàn)中使用的完全公平調(diào)度程序使用紅黑樹。 前言 這篇文章是記錄自己分析 Java 8 的 HashMap 源碼時(shí)遇到的疑問和總結(jié),在分析的過程中筆者把遇到的問題都記錄下來,然后逐一擊破,如果有錯(cuò)誤的地方,希望讀者可以指正,筆者感激不盡。 疑問與解答 什么是 initia...
摘要:在二叉查找樹強(qiáng)制一般要求以外,對(duì)于任何有效的紅黑樹增加了如下的額外要求節(jié)點(diǎn)是紅色或黑色。紅黑樹有哪些應(yīng)用場(chǎng)景內(nèi)核和系統(tǒng)調(diào)用實(shí)現(xiàn)中使用的完全公平調(diào)度程序使用紅黑樹。 前言 這篇文章是記錄自己分析 Java 8 的 HashMap 源碼時(shí)遇到的疑問和總結(jié),在分析的過程中筆者把遇到的問題都記錄下來,然后逐一擊破,如果有錯(cuò)誤的地方,希望讀者可以指正,筆者感激不盡。 疑問與解答 什么是 initia...
閱讀 1444·2021-11-24 10:20
閱讀 3682·2021-11-24 09:38
閱讀 2329·2021-09-27 13:37
閱讀 2236·2021-09-22 15:25
閱讀 2310·2021-09-01 18:33
閱讀 3524·2019-08-30 15:55
閱讀 1819·2019-08-30 15:54
閱讀 2125·2019-08-30 12:50