JDK source code reading (VI): HashMap source code analysis

Recently, many people have asked me add no source of information

In fact, the best is to see official documents, there is a Chinese online API documentation can refer to the website, after all, it is flawed English

blog.fondme.cn/apidoc/jdk-…

To afraid you can not see my face deliberately font package P into the red, do not force the force

Note the text began: JDK version 1.8

HashMap1.8 and very different source before 1.8

  • table of Contents
    • Brief introduction
      • data structure
    • Class structure
    • Attributes
    • Construction method
    • increase
    • delete
    • modify
    • to sum up

1.HashMap Profile

HashMap Map interface based on a hash table, is present in the form of key-value store. (In addition to not allow the use of synchronous and null outside, HashMap Hashtable class is about the same.)
HashMap of implementation is not synchronized, which means that it is not thread safe. It's key, value can be null. In addition, HashMap mapping is not orderly. In JDK1.8, HashMap is composed of an array of linked lists + + red-black tree, adding a red-black tree as the underlying data structure, the structure becomes complicated, but the efficiency becomes more efficient.

1.2 HashMap data structure

In JDK1.8, HashMap is composed of an array of linked lists + + red-black tree, adding a red-black tree as the underlying data structure, the structure becomes complicated, but the efficiency becomes more efficient. When a value is to be stored in the Map when he will be calculated based on the value of the Key

the hash, the hash to be confirmed by the position of the array, if the collision of the hash stored as a linked list 在Object源码分析中解释过, if the list is too long to this but then, the HashMap this list will be converted into a red-black tree stored.

Depending on what point of view of the storage structure HashMap

Source Google Search

But then the question is, why HashMap red-black tree you want to use it, if this structure is not more trouble? ?

I have not thought about this problem, in fact, many of the red-black tree realize only care about when looking into this problem and ignore Why should I use, I am also wondering burst in the time of writing. Reference examples online, but also explains why the threshold is 8:

Because the element initialization Map is stored in the tub chain, which performance is lookup O (n), the tree structure can find performance to O (log (n)). When the chain length was very young, even traverse, speed is very fast, but when the chain length becomes longer continue, will certainly have an impact query performance, so it needs to turn into a tree. As for why the threshold is 8, I think, the source code to find the answer should be the most reliable way.

Reference Address: dwz.cn/nPFXmXwJ

2. Class structure

Let's look at class structure

In reading the source code when there has been a problem that is very confused HashMap has inherited AbstractMap and AbstractMap class implements the Map interface, this same structure in the ArrayList is the LinkedList.

According to the founder of java collections framework Josh Bloch described, this wording is a mistake. In java collections framework, written like this a lot, most began to write java collections framework, he believes wrote, in some places may be valuable, until he realized that was wrong. Obvious, JDK defenders, then do not think this small mistake worth to modify, so there is so down.

  • Cloneable empty interface, represents clone
  • Serialization Serializable
  • AbstractMap implement the interface provided Map

3. Properties

Initial capacity ( must be a power of two )

A collection of the maximum capacity ( must be a power of two )

Load factor, the default 0.75

When the value exceeds 8 list will turn red-black trees ( 1.8 new )

When the value of the list is less than 6 will be reversed from black tree list

When the number Map inside exceeds this value, the table can be a barrel of a tree, otherwise they will be expansion when the barrel too many elements, rather than of a tree to avoid expansion, the tree of choice of conflict, this value can not be less than 4 * TREEIFY_THRESHOLD

4. The method of construction

I began to look constructor.

4.1 HashMap()

Guess you like

Origin juejin.im/post/5d1b0692f265da1baf7d054e