One.Put method
Case 1:
- Click on the topputEnter the underneath
The first step is to get the hashCode value first.
The second step is to get the hash value according to the hashCode value.
The third step is to get a subscript i from the hash value.
The above code is as follows:
The results are as follows:
The fourth step: traversing the hash bucket of the subscript location.
The fifth step is to make a substitution if the reference is equal or equals equal.
2.Click on the tophashEnter the underneath
3.Below map = null; break point.
4.When the breakpoint goes below, it is found that key =”A10″, followed by next, key =”A0″, so A10 and A0 are stored in the [6] location.
One.Put
Case two:
1.
2.Click put to enter the underlying code.
The blue part of the above u1=null is not executed.
If the value has not been saved, put it in addEntry (hash, key, value, I) and return to null.
If the value has been saved to make if judgment, hashCode value is equal and the reference is equal or equals is equal, then the value is equal to replace and return oldValue.
Note: Click e.recordAccess (this) above to enter the following: put the old value in the new value, and put the old value in theNew insidenextReference to point to the old value.。
Note: Click on the top add Entry (hash, key, value, i); go down, when size & gt; = threshold, get the hash value, and double the next 2 * table. length to recalculate the index.
Click on the top createEntry (hash, key, value, bucketIndex) to enter the following.
Take e out (for the old), pass in the old when the new constructor is new (where the key and value are new), and click the Entry method above to go below
The above is next = n.nextRefer to the old one.,This forms a linked list.adopthashCollision can realize chain list.。
Two.Get method
getIn turn, run the key again, get the hash bucket according to the hash, and get the hash barrel according to the re operation.subscript,hashIf the value traverses the value that is not empty, it is judged ifCheck the indexi(Or check the inner linked list e.next),And find the corresponding index value and key value, then return a value as follows:
If the first element key is null, take the first element directly. Otherwise, click the getEntry () method to pass in the key to determine whether size is equal to 0.Entry()There are next in the object.Quote, nextReference will point to another Entry ().object,All Entry is extracted from for traversal and if is used to determine whether hash values are equal.
hashMapPrinciple:
Summary: 1.HashMapThe bottom layer is the array plus linked list, so the array is used because the array memory space is continuous, and the linked list is used because insertion and deletion are faster, only one reference object is needed to point to another reference object.
2.The bottom layer of map only needs to explain the put method when putting a new K and value * (if the key is null in the first position of the hash table, if the key is not hash for null, the value of the key and the length of the hash table do the remainder operation to get the value of the key and the length of the hash table)KeyValue key value, hash location has elements to determine whether there is hash collision.Take the old one out, put the new one in the old one, and point the next in the new application object to the old old old value.This forms a list relationship, and the list is formed; as for how to add, the Entry method is called to determine whether the current hash table is large enough to accommodate the elements to be stored, and if not, to expand the hash table by 2 (n)(Dilatancy efficiencylow,Max 2 (30))The key hash is recalculated, the subscript of the hash table is recaptured, and the ENtry method is called to complete the storage process.
3.getThe method is to operate the key barrel in turn, and then get the hash bucket according to the key and get the hash barrel according to the re operation.subscript,Corresponding correspondence based on subscript traversalEntryLinked list, traversing the list to take out the desired key。Specific:If the first element key is null, take the first element directly, otherwise click the getEntry () method to pass in the key to determine whether size is equal to zero.Entry()There are next in the object.Quote, nextReference will point to another Entry ().object,All Entry is extracted from for traversal and if is used to determine whether hash values are equal.
hashMapThe difference from HashTable:
- mapThread is unsafe; initialization capacity is 16; key value pairs can beempty;High performance; bit operator; maximum capacity expansion 2 (30);
- tableThread safety; initialization capacity11;The key value pair can not be empty.