The principle analysis of JAVA HashMap

Source: Internet
Author: User
Tags final hash int size

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&gt, 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;


     }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.