Implementation details of the Java collection (i) __java

Source: Internet
Author: User
Tags set set
Set and MapThrough the inheritance relationship system, we can draw the Set<->map enumset<->enummap sortedset<->sortedmap treeset<->treemap Navigableset<->navigablemap Hashset<->hashmap Linkedhashset<->linkehashmap on the surface of these two sets is not much of a relationship, But if you only consider the key of the map set, it is not hard to find that the key of these map sets has a feature: All keys cannot be duplicated, no order. All the keys in the map collection are set together, which is a set set. The Map collection provides a way to return a set set of all keys. Set<k>keyset (); For a map collection, it's actually an associative array. Map can always query the corresponding value according to the key fast, then the map collection can only consider key when saving key-value. As long as the traditional set is slightly modified, the set can be transformed into a map set, which is almost comparable to the map provided by the system.
HashMap and HashSetThere are many similarities between HashSet and HashMap, for the HashSet, the system uses the hash algorithm to determine the storage location of the set elements, which can guarantee the fast saving and fetching of the collection elements. For HashMap, the system will value as a key, the system based on the hash algorithm to determine the storage location of key, so that you can quickly save, take the collection key, and value is always immediately followed by key storage. Although a collection is called a Java object, it does not actually put a Java object in the set collection, but only a reference to a set set that retains those objects, and the Java collection is actually a collection of reference variables that point to the actual Java object. Arrays of reference types, when you put a Java object into an array, you don't actually put a Java object in an array, but just put the object's references into the array, and each array element is a reference variable. Entry has the best performance when the entry stored in each bucket of the HashMap is only a single entry and no HashMap chain is generated through the pointer. When the program takes out the corresponding value by key, the system only calculates the hashcode () value of the key, then finds the index of the key in the table in the Hashcode value, then pulls out the entry of the index and returns the value of the key. In the case of a "hash conflict", a single bucket exists not a entry value but a entry chain, and the system must traverse each entry chain sequentially until it finds the entry to search for. If the entry that you just want to search is at the very end of the entry chain, you must loop to the best to find the element.
HashMap at the bottom of the key-value as a whole to deal with, this whole is a entry object. The HashMap bottom uses a entry[] array to hold all the key-value pairs, and when a entry object needs to be stored, its storage location is determined according to the hash algorithm. When you need to remove a entry, it also finds its storage location according to the hash algorithm and takes out the entry directly. The reason why HashMap can quickly save and take the entry it contains is exactly the same as in real life: different things need to be placed in different places to find it quickly when needed.
When HashMap is created, there is a default load factor with a default value of 0.75. This is a trade-off between time and space costs: increasing the load factor can reduce the memory occupied by the hash table, but it will increase the time cost of querying the data, while the most frequent operations (put and get methods need to use the query), reducing the load factor will improve the data query performance, But it reduces the amount of memory space that the hash table occupies.
The value of the load factor is adjusted to the actual needs when the HashMap is created. If the program is more concerned about the space overhead, memory is more nervous, you can increase the load factor appropriately. If the program is concerned about the time overhead, memory is relatively well-off, you can reduce the load factor. In general, you do not need to change the load factor.
If you begin to know that HashMap will save multiple key-value values, you can use a large initialization capacity at the time of creation, and if the number of entry in HashMap does not exceed the limit capacity, HashMap will not need to call the resize () method to reassign the table group , so as to ensure better performance. Of course, setting the initial capacity too high at first can be a waste of space. Therefore, the initialization of capacity settings when creating HashMap also requires careful treatment.

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.