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

How to understand equals and hashCode

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to understand equals and hashCode, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

. The difference between = = and equals

= = is often used to compare values of basic data types, and we usually use the equals method when comparing objects. We know that all classes inherit from the Object class. In fact, the equals method in Object uses = = to compare.

That is, = = can also be used to compare objects, as in the Object class. But if you compare objects with a double equal sign, you compare the address of the object in memory, not the value. So generally in our business scenario, for example:

1public class House {

2 private int houseShape;// house type

3 pirvate long houseSize;// house size

4 private int houseArea; / / House lot

5...

6}

For example, the above class House.java, if we compare that two House are equal. It should be considered that the two houses are equivalent when the location, size, apartment type and other properties of his house are equal.

So for the comparison of this type of objects, it is obviously not appropriate to use a double equal sign, so we should override the equals method:

1public class House {

2 private int houseShape;// house type

3 pirvate long houseSize;// house size

4 private int houseArea; / / House lot

5...

six

7 @ Override

8 public boolean equals (Object obj) {

9 if (obj instanceof House) {

10 House p = (House) obj

11 return this.houseShape = = p.houseShap

12 & & this.houseSize = = p.houseSize

13 & & this.houseArea = = p.houseArea

14}

15 return super.equals (obj)

16}

17}

. Is it necessary to rewrite equals if you rewrite hashCode?

The answer is no!

First of all, hashCode can be understood as the address of the object (the hash code generated based on the address). As long as the new comes out of an object, the hashCode should be different. In general, we really don't care about the hashCode method. But when we need to store objects in some data structures, such as HashSet, when we rewrite equals, we have to rewrite hashCode. Why is that? Because in this data structure, it is not allowed to store two equal objects repeatedly, how can it judge whether two objects are the same? Some people say that you can use the equals method. There is no need for hashCode at all. Yes, you can tell by using the equals method. But have you ever thought that if there are tens of millions of pieces of data, each additional piece of data will be compared with all the previous data through the equals method? Definitely not, so the master who designed the HashSet structure thought of judging by the address of the object, there must be only one object in the same address, as long as there are new objects added and directly find the corresponding address of the memory. Because the master has made such an ingenious design, when we want to put the data into the data structure of the Hash structure, we must make sure that when we use equals to determine that the object is equal, we should have the same memory address, that is, the same hashCode.

On the contrary, when the equals judges that the object is not equal, its hashCode should not be equal. So we rewrite both the equals method and the hashCode method to ensure this.

After reading the above, have you mastered how to understand equals and hashCode? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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