Let's take a look at the structure of the hashmap as a whole. As shown in the following figure (the picture is not good), the box represents the hash bucket, the ellipse represents the elements in the barrel, here is the key-value to the composition of the map.entry image.
If there are multiple elements of the hash function to locate the same bucket, we call it a hash conflict, the element in the bucket is composed of one-way linked list. Let's take a look at the HashMap JDK source code (due to the length of the relationship, delete some of the codes and comments, can be viewed JDK1.6 source):
public class hashmap<k,v>
extends Abstractmap<k,v>
implements Map<k,v>, Cloneable, Serializable
{
static final int default_initial_capacity = 16;
static final int maximum_capacity = 1 << 30;
static final float default_load_factor = 0.75f;
transient entry[] table;
transient int size;
int threshold;
final float Loadfactor;
transient volatile int modcount;
public HashMap (int initialcapacity, float loadfactor) {
if (initialcapacity < 0)
throw new IllegalArgumentException ("Illegal initial capacity:" +
initialcapacity);
if (initialcapacity > Maximum_capacity)
initialcapacity = maximum_capacity;
if (loadfactor <= 0 | | Float.isnan (loadfactor))
throw new IllegalArgumentException ("Illegal load factor:" +
loadfactor);
//Find a power of 2 >= initialcapacity
int capacity = 1;
while (capacity < initialcapacity)
capacity <<= 1;
this.loadfactor = Loadfactor;
threshold = (int) (capacity * loadfactor);
table = new Entry[capacity];
init ();
}
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;
}
private V Getfornullkey () {
for (entry<k,v> e = table[0]; e!= null; e = e.next) {
if (E.key = null)
return e.value;
}
return null;
}
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;
}
Private v Putfornullkey (v value) {
for (entry<k,v> e = table[0]; e!= null; e = e.next) {
if (E.key = = null) {
V oldValue = e.value;
E.value = value;
e.recordaccess (this);
return oldValue;
}
}
modcount++;
addentry (0, NULL, value, 0);
return null;
}
}