JDK HashMap and HashSet analysis, jdkhashmaphashset

Source: Internet
Author: User

JDK HashMap and HashSet analysis, jdkhashmaphashset

HashMap mainly analyzes the operations of putting key and value into Map and retrieving Map, as well as its traversal tool. I personally think that there is an important internal class Entry in HashMap, And the put and get methods of Map depend on this Entry. First, let's analyze this internal class Entry. The Entry contains several important variables: key, value, and next. Needless to say, everyone will understand the meanings of these variables, of course, there will naturally be get and set methods. When traversing a Map, we can obtain the object entry of this internal class and then obtain the value from this entry. Now let's take this example to traverse a Map. First obtain the Map entrySet, and then obtain the iterator: map. entrySet (). iterator (), obtain the Entry using the next method, and get the Value in the Entry. In this way, obtain the Entry object of map first, and then get the key and value. Different from another Traversal method: map. keySet (). iterator (), which obtains the set of keys first and then obtains the value through it. The two methods can traverse the entire Map at last, but the implementation methods are different and the internal classes used are different. The traversal tool is generated in the internal class. To use the traversal tool, you must use an internal class. In fact, internal classes in the class cannot be directly called in the Map, and they all use methods in the class, return the internal class object in the method to manipulate the internal class method. Many classes in the JDK source code perform this operation, which effectively hides the internal class, however, I have not yet had a deep understanding of the advantages of this practice. I just think that this method uses the java proxy mode and implements multiple inheritance of java (PS: hope you can give me the benefits and significance of using internal classes ). An important method in MAP is put. Put is to put the key and value into the MAP. First, he will get the map to traverse the map, get the Entry, and traverse the map. If the value is equal to the current value, the value in this entry will be replaced and the current entry will be returned. In this case, the value corresponding to the key value in the map will always have only one value, which is the latest. If not, put the current key and value into the entry. In put, map provides the putForNullKey method, that is, the key is allowed to be null. If the key is null, the key is set to null, just like normal put, only when comparing whether a null key exists, you do not need to compare its hash value. Of course, putForNullKey is private and can only be called through put. Map also providesContainsValue andThe implementation logic of the containsKey method is basically the same. They are all Map variables, get the Entry, and then compare the key or value. In the API, there are two parameters that affect the performance of Map: initial capacity and loading factor. This does not know much about it and requires further analysis.

In fact, the underlying implementation of HashSet depends on HashMap, or its method implementation calls the HashMap method. For example, the HashSet constructor creates a new HashMap. When an element is added to the Set, the put value in the Map is used, except that the value is Set to PRESENT, the PRESENT is a global object type object. It can be said that Set is actually a Map, but the values of this Map are the same. It is a root object, but the key is different. The reason is that there are no duplicate values in the set is that the key in the Map is unique. If there are duplicate keys, the value is replaced, and the values are the same here, the key must be unique. When the same element is added to the set, no exception processing is required, because the put implementation method of map determines that no exception will occur. Other methods of set do not need to be analyzed too much. Basically, they call methods in map. For the set iterator, it uses the traversal map method to get the second map. keySet (). iterator () described above, so as to get the map iterator and iterate the set. To better understand set, you only need to understand map.

The reason why HashMap and HashSet are put together is clear. In fact, this article is about analyzing HashMap. (PS: The main analysis here is the commonly used methods in Map work, there may be many interesting and useful methods not analyzed, when used, you need to take a good look at the source code, ).

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.