Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why rewrite equals and hashcode

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "Why rewrite equals and hashcode". In daily operation, I believe many people have doubts about why to rewrite equals and hashcode. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "Why rewrite equals and hashcode"! Next, please follow the editor to study!

First of all, let's explain what the equals () method and the hashCode () method are used to do respectively.

Let's look at the relationship between the equals () method and the hashCode () method.

Finally, why do you have to override the hashCode () method when you overwrite equals ()?

1. First of all, let's explain what the equals () method and the hashCode () method are used to do respectively.

Equals () method:

Obviously, this method is used to determine whether two objects are the same object. In the Object class source code (shown below), the bottom layer is implemented using "=", that is, to determine whether it is the same object by comparing whether the memory addresses of two objects are the same.

Public boolean equals (Object obj) {return (this = = obj);}

But in practical application, this method can not meet our needs. Because we think that even if the two objects do not point to the same block of memory, as long as the field property values of the two objects are the same, then the two objects are considered to be the same object. So you need to override the equals () method, that is, if two objects point to the same memory address or if each field of the two objects has the same value, then it is the same object.

HashCode () method:

When you mention hashcode, you naturally think of a hash table. Map a key value to a location in the table to query the key value with O (1) time complexity. In the Object class source code (shown below), hashCode () is a native method, and the hash is calculated using the memory address.

Public native int hashCode ()

It can be considered that the use of hash table can also play a certain role in judging weight, but the reality is that there may be a hash conflict, even if two different objects, their hash value may be the same, how to solve the hash conflict, here a little bit. In summary, we remember that hash tables have superior query performance and there are hash conflicts.

2. What is the relationship between the equals () method and the hashCode () method?

The factual relationship between the two is as follows:

If two objects are the same (that is, true is returned by equals comparison), then their hashCode values must be the same!

If two objects are different (that is, equals comparison returns true), their hashcode values may be the same or different

If the hashCode of two objects is the same (there is a hash conflict), they may be the same or different (that is, the equals comparison may be false or true)

If the hashCode of two objects is different, then they must be different (that is, equals comparison returns false)

3. Finally, why do you have to override the hashCode () method when you overwrite equals ()?

For the weight judgment of an object set, if a set contains 10000 object instances and only uses the equals () method, then the weight judgment for an object needs to be compared 10000 times. With the increase of the size of the set, the time cost is very large. But if you use the hash table at the same time, you can quickly locate the approximate storage location of the object, and after locating to the approximate storage location, if the hashCode of the two objects is different, you no longer need to call the equals () method, thus greatly reducing the number of equals () comparisons. So from the principle of program implementation, we need both the equals () method and the hashCode () method. So now that you have overridden equals (), you should also override the hashCode () method to ensure a matching relationship between the two.

There are three ways to solve hash conflicts: open addressing method: this method is also called rehashing method. Its basic idea is: when the hash address pairh (key) of the keyword key conflicts, another hash address p1 is generated based on p, and if p1 is still conflicted, another hash address p2 is generated based on p. Until a non-conflicting hash address pi is found and the corresponding element is stored in it This method has a general form of rehashing function: linear detection and hashing: this method is characterized by sequentially checking the next unit in the table when a conflict occurs until an empty unit is found or the table is searched all over the table. The characteristic of the method of secondary detection and hashing is that when a conflict occurs, jump detection is carried out on the left and right side of the table, which is more flexible. At this point, the study of "Why rewrite equals and hashcode" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report