Java uses a custom class as the key value instance of HashMap

This is a classic question in Java and is often asked in interviews. In fact, many books or articles have mentioned that it is necessary to overload hashcode () and equals () methods to realize the search of user-defined keys in HashMap, but why and what consequences will result if you don't do so? It seems that few articles talk about it, so write this article to explain.

First, what happens if we directly use the following person class as the key and store it in the HashMap?

So what is the output?

We can see that there are two problems:

1. In the process of adding, we added the key value pair of key = new person ("003") twice. In expectation, there should be only one such key value pair in HashMap, Because the key (expected) is the same, it should not be added repeatedly. The value = "findingsealy" added the second time should replace the original value = "Henry". However, in the input, we find that the expected situation does not appear, but there are both value = "findingsealy" and value = "Henry" in the HashMap And their key values are still different, which is obviously wrong.

2. When getting the value value, we use three person objects to find it. These three objects are the same as the three key values we just stored (expected), but we find three null values, which is obviously wrong.

In fact, the correct method has been described in many places. Directly modify the person class and overload the equals and hashcode methods. The modified person class is as follows:

Then, when we re execute the above inspection procedure, the results are as follows:

It can be seen that the bright spots and errors pointed out before have been corrected. So why?

In HashMap, the comparison order of finding keys is:

1. Calculate the hash code of the object to see if it exists in the table.

2. Check whether the object in the corresponding hash code position is equal to the current object.

Obviously, the first step is to use the hashcode () method, and the second step is to use the equals () method. When there is no overload, these two methods of the object class will be called by default in these two steps. In object, the calculation method of hash code is calculated according to the address of the object. The object addresses of the two persons ("003") are different, so their hash codes are also different. Naturally, HashMap will not regard them as the same key. At the same time, in the default equals () of object, the comparison is also based on the address of the object. Naturally, one person ("003") and another person ("003") are not equal.

With this in mind, it's easy to figure out why you need to overload both hashcode () and equals methods at the same time.

• reloading hashcode () is to get the same hash code for the same key, so that the HashMap can locate the key specified by us.

• overloading equals () is to indicate to HashMap that the current object and the object saved on the key are equal, so that we can really get the key value pair corresponding to the key.

Another detail is that in the person class, the key methods for hashcode () are:

There may be doubts here: why can the hash code of a variable of type string be used as the hash code value of the person class? Is the hash code of new person (new string ("003")) and new person (new string ("003")) equal?

Take a look at the output of the following code:

You can see that the outputs of the four statements are equal. A very intuitive and reasonable guess is that the string type also overloads hashcode () to return the hash code value according to the content of the string, so strings with the same content have the same hash code.

At the same time, this also explains a problem: why do you need to compare with equals () when you know that hashcode () is equal? This is to avoid the situation in the above example. According to the overloaded implementation of the hashcode () method of the person class, the person class will directly use the hash code value of the string member ID as its own hash code value. However, obviously, a person ("003") and a string ("003") are not equal, so when the hashcode () is equal, You also need to compare with equals ().

The following examples can be used as evidence of the above description:

The above article on Java using custom classes as key value examples of HashMap is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>