Terminology Explanation:
Load factor:
The load factor indicates how full the hash table is, defined as the number of nodes in the hash divided by the number of nodes that the base area can hold. default_load_factor=0.75f, when 75% of the space in the array is already in use, it will reopen a new array, expanding to twice times the original, copying the elements from the original array into the new array.
1, the bottom of the hashset is implemented using HASHMAP. When you add an object to a set using the Add method, the object is actually the key of the map object that is being maintained at the bottom, and value is the same object object (which we cannot use)
Public boolean Add (E e) {
Return Map.put (E, PRESENT) ==null;
}
Dummy value to associate with a Object in the backing Map
private static final Object PRESENT = new Object (); 2, HashMap the bottom of the maintenance of an array, we to the HashMap placed in the object is actually stored in the number
Group;
Public HashMap () {
This.loadfactor = Default_load_factor;
threshold = (int) (default_initial_capacity * default_load_factor);
Table = new Entry[default_initial_capacity];
Init ();
}
3. When you put a pair of key values into the HASHMAP, it calculates a position based on the hashcode value of the key, which is where this object is intended to be stored in the array.
Public V-Put (K key, V value) {
if (key = null)
return Putfornullkey (value);
int hash = hash (Key.hashcode ());
int i = indexfor (hash, table.length);
for (entry<k,v> e = table[i]; e!= null; e = e.next) {
Object K;
if (E.hash = = Hash && ((k = e.key) = = Key | | key.equals (k))) {
V oldValue = E.value;
E.value = value;
E.recordaccess (this);
return oldValue;
}
}
modcount++;
AddEntry (hash, key, value, I);
return null;
}
4, if there is no object in the position, the object is placed directly into the array, and if there is already an object in place, it is searched along the chain of the existing object (the Entry class has a Entry type next member variable pointing to the object's next object), if there is an object on the chain, To compare using the Equals method, if the Equals method of an object on this chain is compared to false, the object is placed in the array, and then the object that is previously present in the array is linked to the back of the object (the object or element that is currently being used). is most likely to be used in the near future)
5, HashMap memory to achieve the layout: