程式設計師英語-HashMap源碼解讀

2020-12-25 春蟲愛學習

本期關鍵詞:

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集合框架中的一員。

相關焦點

  • Java面試高頻考點:HashMap的底層原理
    作為一個Java開發工程師,在面試的過程中,最高頻被問到的一個問題就是:「請簡述一下HashMap的實現原理」,在日常開發中,大多數程式設計師只會使用,對於其實現細節,卻不了解,殊不知這是較基礎卻也最重要的知識點。這篇文章將向大家詳細解釋hashmap的底層到底做了哪些事情。
  • 3.HashMap源碼剖析
    是有可能出現瘸子的情況,只有一條腿,不平衡了,導致查詢性能變成O(n),線性查詢了紅黑樹,紅色和黑色兩種節點,會有條件限制去保證樹是平衡的,不會出現瘸腿的情況如果插入節點的時候破壞了紅黑樹的規則和平衡,會自動重新平衡,變色(紅 <-> 黑),旋轉,左旋轉,右旋轉4.1 如果要完全搞得紅黑樹,還是需要花點時間和精力的,我們研究HashMap的話,重點放在源碼上
  • HashMap源碼閱讀
    HashMap源碼閱讀本文基於JDK1.8  >讀完本文預計需要25分鐘(因有大量原始碼,電腦屏觀看體驗較佳)摘要HashMap相信這是出現頻率最高的面試點之一,應該是面試問到爛的面試題之一,同時也是Java中用於處理鍵值對最常用的數據類型。那麼我們就針對JDK8的HashMap共同學習一下!
  • HashMap容量為什麼必須是2的k次方
    不提版本就分析源碼的都是耍流氓,因此事先聲明,這裡分析的源碼都是基於jdk8,別擔心,全文源碼不超過200字符首先看一下hash算法的源碼: 很簡單,如果key為null,哈希值就是0,否則就將hashcode和hashcode右移16位之後的值做異或,因此hashmap是允許鍵為null的,但只能有一個鍵為null的元素(獎勵一道面試題)。
  • 英語在程式設計師的工作中究竟扮演著什麼樣的角色?
    每晚準時上線的博醬來了~話說大家的英語是個什麼樣的水平呀?在學習編程的時候你的英語實力是拖後腿子啦還是助力啦?英語對程式設計師來說重要嗎?也就是說,如果完成基礎工作就是目的,英語水平過及格線即可~反之,如果不想一直只是個初級程式設計師,還想進階成為中高級層次,僅僅及格水平的英語可滿足不了我們的工作需求。
  • 教你用 Python 實現 HashMap 數據結構
    hashmap和hash算法究竟是什麼關係?hashmap有哪些參數,這些參數分別是做什麼用的?hashmap是線程安全的嗎?我們怎麼來維護hashmap的平衡呢?讓我們帶著疑問來看看hashmap的基本結構。
  • Java程式設計師需要什麼學歷才能找到工作?學歷低怎麼辦?
    IT行業薪資很高,很多人只看到了這點,沒發現非常多的人也奔著想拿高薪的念頭往這個行業擠,這麼多人,企業當然要高個子中挑更高的了各種限制也因此而來,學歷,工作經驗,掌握的技能等等Java程式設計師需要什麼學歷才能找到工作?
  • 程式設計師是如何閱讀源碼的
    最近寫了很多源碼分析相關的文章,React、Vue 都有,想把我閱讀源碼的一些心得分享給大家。調試 React這裡我們先拿 React 舉例,把源碼 clone 下之後,整個人都懵逼了。
  • Java WeakHashMap 源碼解析
    Reference類繼承關係下面分析下Reference的源碼(其他三種引用都是其子類,區分不是很大)。對於一般程式設計師來說,這四種狀態完全可以不用管。最後簡單兩句話總結上面的四種狀態:如果構造函數中指定了ReferenceQueue,那麼事後程式設計師可以通過該隊列清理引用如果構造函數中沒有指定了ReferenceQueue,那麼 GC 會自動清理引用get調用Reference.get方法可以得到該引用指向的對象,但是由於指向的對象隨時可能被 GC 清理,所以即使在同一個線程中
  • 好程式設計師前端:MongoDB資料庫全套教程(精華版 含源碼)
    為了方便大家學習,好程式設計師一直堅持免費為大家分享IT教程,就是為了能夠讓更多人享受到優質的編程學習資源。今天,又到了好程式設計師視頻分享日,喜愛學習的小夥伴有福啦!小小媛今天要為大家分享的教程是:好程式設計師HTML5大前端精品—MongoDB資料庫全套教程(精華版),還是老規矩,學習資源包含:全套視頻+源碼+筆記!想學的同學,可以在文章末尾免費領取!手慢無!抓緊時間!
  • 程式設計師竊取公司《傳奇霸業》源碼,被判刑
    又有程式設計師因竊取公司軟體源碼而被判刑。
  • 細讀源碼之Java HashMap(一)
    細讀源碼是《馬士兵教育》旗下賞析源碼的欄目。
  • 全民編程時代,程式設計師該如何保住飯碗?
    所以我不認同那些認為世界上只需要幾百萬程式設計師的觀點,在我看來,(幾乎)每個人都應該學習編程,就像每個人都應該學習閱讀和寫作一樣。」那麼在全民學編程的時代,作為程式設計師的我們,該如何保住飯碗呢?全民編程時代,程式設計師該如何保住飯碗目前學編程和幾十年前的英語學習很像,之前英語專業是個香餑餑
  • 如何閱讀Java源碼?
    閱讀Java源碼的前提條件:1、技術基礎在閱讀源碼之前,我們要有一定程度的技術基礎的支持。比如設計模式,許多Java源碼當中都會涉及到。再比如閱讀Spring源碼的時候,勢必要先對IOC,AOP,Java動態代理等知識點有所了解。2、強烈的求知慾強烈的求知慾是閱讀源碼的核心動力!
  • 初級程式設計師、中級程式設計師,高級程式設計師是如何定義的?
    我將程式設計師分成三個級別:初級程式設計師能夠獨立完成一個項目中級程式設計師能夠了解一些框架原理,做出一些改進和優化>高級程式設計師能夠寫一些框架,甚至一個新語言在具體分析各個級別程式設計師的定義的時候,我們先來想一下,大部分的程式設計師來源於:學校、自學和培訓機構。
  • HashMap是如何工作的
    同時, 由於 hashmap 不能自動的縮小容量 因此,如果你的 hashmap 容量很大,但執行了很多 remove操作時,容量並不會減少。如果你覺得需要減少容量,請重新創建一個 hashmap。八、HashMap.put() 函數內部是如何工作的?
  • 阿里螞蟻金服Java程式設計師面試的11個問題,你會幾個呢?
    此前,w3cschool app開發者頭條上分享了各種的名企程式設計師面經。比如阿里巴巴java面經、小米java面經、網易java面經等,吸引了大多數的程式設計師們的圍觀。在分享螞蟻金服Java程式設計師面經前,不妨來看下Java程式設計師面試時要注意3大要點:0、重視基礎在面試之前,有必要將基礎的知識點重新過一遍,比如並發優缺點、內存可見性、鎖、同步、線程池框架等。
  • 拼多多JDK源碼大揭秘,由淺入深看源碼,探究Java並發原理
    寫在前面幾乎所有的大神都會強調看源碼,也強調源碼的重要性;但是如何看源碼,源碼看什麼?看了什麼用?看了怎麼用?困擾很多人,尤其是初學者。如何閱讀源碼,是每個程式設計師需要面臨的一項挑戰。為什麼需要閱讀源碼?
  • 吃透這篇ConcurrentHashMap源碼,就夠了
    核心存儲結構是segment,它是繼承ReentrantLock的源碼如下:static class Segment<K,V> extends ReentrantLock implements Serializable
  • 為啥國內程式設計師寫的代碼也用英文注釋?
    國內的一些程式設計師寫代碼用英文進行注釋,我覺得是他們編程之路上的一大重要進步。中國編程技術現在需要與國際接軌,而且國內很多程式設計師也喜歡更多了解程式語言的開原始碼,使用英文注釋會讓國內程式設計師在全球技術社區中形成廣泛共識和合作,從而使國內程式設計師能夠實現快速成長。