HashMap for Java Collection Framework Learning

Source: Internet
Author: User

HashMap for Java Collection Framework Learning
The following features of HashMap can be obtained from the API documentation: implemented based on the hash table, and the chain hash table allows null values and null keys (null = null key-value pairs) hashMap and Hashtable are basically the same. The difference is that HashMap is non-synchronous and non-thread-safe, and can support unordered HashMap with null values: HashMap does not guarantee the order of elements, it is not guaranteed that the order of elements will remain unchanged O (1) time Efficiency: it provides a constant time (constant-time) for basic get and put operations) two parameters that affect HashMap performance: initial capacity and load factor ). Do not set the initial capacity too large or the load factor too small. To better balance time and space consumption, the default load factor is generally 0.75. The default capacity is 16. When the entry (key-Value Pair) in the hash table exceeds the threshold (threshold = capacity * factor), the hash table is rehash ). The number of buckets in the hash table after hash is twice. Select Hashtable from jdk1.0, while HashMap is added from jdk1.2. ConcurrentHashMap is provided in jdk1.5. Both Hashtable and ConcurrentHashMap are thread-safe. However, ConcurrentHashMap is a more advanced concurrency tool added by 1.5. It uses the segment lock technology to achieve more fine-grained synchronization. Therefore, ConcurrentHashMap is more efficient than Hashtable. Therefore, ConcurrentHashMap is generally used in multithreading. HashMap is non-thread-safe, so it is generally used in a single thread. Priority: multi-threaded access: ConcurrentHashMap. Single-threaded access: write a simple and easy-to-understand code for HashMap verification. The Code is as follows, and then set a breakpoint before two lines of code with comments: public class HashMapL {public static void main (String [] args) {HashMap <Integer, Integer> hashMap = new HashMap <> (); hashMap. put (null, null); // test null key and null value; for (int I = 0; I <16; I ++) {hashMap. put (I, I); // autoboxing} run the above Code in debug mode. We can see that HashMap hashMap = new HashMap <> (); The following hashMap is initialized, the default value of the load factor loadfactor is 0.75. Note that the current value of threshold is 0. According to the comments in the HashMap source code, this value is calculated only after the table is allocated space. The value before the allocation is 0, currently, the table value is null and has not been allocated. So what is the table here? People who know about the chained hash table will easily know that it is a linked list array. Press F6 to perform the next step, assign a value to hashMap, and use a null value to verify the difference with Hashtable: NULL values can be saved. Now that table has been initialized, it now has an element "null = null" (which can be viewed at the bottom of the image ). It is a Node-type array containing 16 elements. Remember, table id = 24 can be compared with tableid after hash. Continue to press F6 until size = threshold = 12. At this moment, the table id is the same as the previous one, or id = 24. Now, press F6 again to execute the next step and add an element to hashMap, make the total size of elements greater than the threshold value threshold. We can see from the image that the table id = 104, the size is 32, and the threshold value threshold = 24. The previous id = 24, the size is 16, threshold = 12. Therefore, it is concluded that when the element size is greater than the threshold value threshold, it will be re-hashed. After hash, The HashMap is automatically expanded to 2 times the previous one. And replace the original object with a new object. It can also be learned that automatic resizing consumes resources and should minimize the occurrence of automatic resizing.

Related Article

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.