Java HASHMAP data structure and put and get methods __arcinfo

Source: Internet
Author: User

1 data structure of HASHMAP

HashMap is actually an array of linked lists, the outermost is an array, and the elements of the array are linked lists.

2 HashMap's Put method:

The source code is as follows:

<span style= "font-size:10px;"  >    Public v put (K key, V value) {       //1 If key is null put to key null position    
    if (key = null)             return Putfornullkey (value);
       //2 calculation key hash value         int hash = hash (Key.hashcode ());
       //3 get index position         int i = indexfor (hash, table.length);        //4 traversing the list of index locations, overwriting and returning old values if they exist, and if not, create a new location insert         for (ENTRY&LT;K,V&G T e = Table[i]; e!= null; E = e.next) {            Object K;            //4.1 key location has been The old value is given the new value and return the old value             if (E.hash = = Hash && (k = e.key) = = Key | | key.equ ALS (k)) {                V oldValue = e.value         &NBSP ;       E.value = value;
                e.recordaccess (this);                                  Retu
RN OldValue;             {       }        //4.2 call a If key is new
Ddentry inserts new key and value         modcount++;
        addentry (hash, key, value, I);
        return null;    }</span>

3 source code is as follows HashMap get method:

The source code is as follows:

<span style= "font-size:10px;" > Public    V get (Object key) {
        //1 if key is null to return the value of the key null directly if
        (key = null) returns
            Getfornullkey ();
        //2 calculates the hash value of the key object
        int hash = hash (Key.hashcode ());
        for (entry<k,v> e = table[indexfor (hash, table.length)];//Find the index e!= null by calculating the hash of the key
             ;
             E = e.next) {//Find traversal list Object K in index
            ;
            Find key position by equals comparison if key is found and returns value if
            (E.hash = Hash && ((k = e.key) = = Key | | key.equals (k)) return
                E.value;
        }
        return null;
    } </span>


Analysis of 4 Concurrenthashmap:

Mainly to divide the segment

Resources:

Java Improvement article (two or three)-----HASHMAP

The Concurrenthashmap of source analysis

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.