Javase_ insist read source _hashmap object _get_java1.7

Source: Internet
Author: User

When you get from HashMap, what are you actually doing?
1 /**2 * Returns the value to which the specified key is mapped,3 * or {@codeNULL} If this maps contains no mapping for the key.4      *5 * <p>more Formally, if this map contains a mapping from a key6      * {@codek} to a value {@codeV} such that {@code(key==null? K==null:7 * Key.equals (k))}, then this method returns {@codev}; otherwise8 * It returns {@codenull}. (There can is at the most one such mapping.)9      *Ten * <p>a return value of {@codenull} does not <i>necessarily</i> One * indicate that the map contains No. mapping for the key; it ' s also A * Possible that the map explicitly maps the key to {@codenull}. - * The {@link#containsKey ContainsKey} operation is used to - * Distinguish these, cases. the      * -      * @see#put (Object, Object) -      */ -      PublicV get (Object key) { +         //If key is null, call Getfornullkey, this method will fetch the table[0] element -         if(Key = =NULL) +             returnGetfornullkey (); A         //Otherwise, the Getentry method is called according to key atEntry<k,v> Entry =getentry (key); -  -         return NULL= = entry?NULL: Entry.getvalue (); -}

1 /**2 * Returns the entry associated with the specified key in the3 * HashMap. Returns NULL if the HASHMAP contains no mapping4 * for the key.5      */6     FinalEntry<k,v>getentry (Object key) {7     //this size is the number of key-value mappings in the current map8         if(Size = = 0) {9             return NULL;Ten         } One      A     //The key is also under non-null check, if not empty, through the hash () to get the key corresponding hash value -         inthash = (Key = =NULL) ? 0: Hash (key); -     //Remove the value at the table at the specified index, if the hash and key can match the          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! =NULL&&key.equals (k) ))) +                 returne; A         } at         return NULL; -}

As can be seen from the above code, if there is only one Entry in each bucket of HashMap, HashMap can quickly take out the Entry in the bucket according to the index, and in the case of "Hash conflict", the single bucket is not stored in an E Ntry, instead of a Entry chain, the system must traverse each Entry sequentially until it finds the Entry to search for--if the Entry that happens to be searched is at the very end of the Entry chain (the Entry is first placed in the bucket), the system must loop to the Before you can find the element.

summed up simply, HashMap at the bottom of the key-value as a whole to deal with, this whole is a Entry object. HashMap the bottom of a entry[] array to hold all key-value pairs, when a Entry object needs to be stored, according to the hash algorithm to determine its storage location, when the need to remove a Entry, the hash algorithm will also find its storage location, directly take out The Entry. Thus: HashMap is able to quickly save, take it contains the Entry, exactly like the real life of the mother taught us: different things to put in different locations, when needed to quickly find it.

----------------------- load factor (Loadfactor) I'm not getting it, I'm going to study it------------------------------

When creating a HashMap, there is a default load factor (load factor) with a default value of 0.75, which is a tradeoff between time and space costs: increasing the load factor can reduce the memory footprint of the Hash table (which is the Entry array), but increases the time overhead of querying the data , and the query is the most frequent operation (theHashMap get () and the Put () method all use the query ); Reducing the load factor will improve the performance of the data query, but will increase the memory space occupied by the Hash table.

Having mastered the above knowledge, we can adjust the value of load factor according to the actual need when we create HASHMAP, if the program is concerned about space overhead, memory is more tense, the load factor can be increased appropriately, if the program is more concerned about the time overhead, A more comfortable memory can reduce the load factor appropriately. Typically, programmers do not need to change the value of the load factor.

----------------------- load factor (Loadfactor) I'm not getting it, I'm going to study it ------------------------------

If you start to know that HashMap will save multiple key-value pairs, you can use a large initialization capacity at creation time, if the number of Entry in HashMap never exceeds the limit capacity (capacity * load factor), HASHMAP does not need to call The resize () method re-allocates the table array to ensure good performance. Of course, starting to set the initial capacity too high can be a waste of space (the system needs to create a Entry array of length capacity), so initializing the capacity setting when creating HashMap also requires careful treatment.

Javase_ insist read source _hashmap object _get_java1.7

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.