Then, in the previous section, let's discuss the Get method.
1. Use the diagram from the previous section
Referenced from: http://www.admin10000.com/document/3322.html
Let's take a simple step by finding the position above the table by Hashcode and then traversing the linked list on the position.
Source code for 2.get method:
Public V get (Object key) { if (key = = null) return Getfornullkey (); int hash = hash (Key.hashcode ()); for (entry<k,v> e = table[indexfor (hash, table.length)]; E! = null; E = e.next) { Object k; if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k)) return e.value; return null; }
Explain:
(1) When key is empty, use a specific method to get the element
Source code for Getfornullkey:
Private V Getfornullkey () {for (entry<k,v> e = table[0]; E! = null; e = e.next) { if (E.key = = null) RE Turn e.value; } return null; }
From the source above, E=table[0],e is to take the first element of table, once again to prove our previous conclusion, key is null element, is directly placed in the first position of the table.
(2) Calculate the hashcode of the key and then use the hash method.
(3) Find the position above the table
(4) Traverse the linked list, find the corresponding element (in addition to the comparison Hashcode, but also through the Equals method of comparison)
Summary: This section describes the relatively simple, because basically get is put the inverse operation.
This chapter is here, thank you.
-----------------------------------
Directory
java-15.7 Map (3)-Introduction to how HashMap works-get method