Interviewer's Favorite Pitfall: Why must I rewrite hashCode when rewriting equals?
AD |
#Autumn Life Clock In Season#Hello, I'm Xiaomi. Welcome to my WeChat official account! Today, we will discuss a frequently asked question in Java development: "Why is rewritingEqualsMethod also needs to be rewrittenHashCodeMethod This question may appear frequently in your interviews, but it is not just an interview question, it also involves very important concepts in Java, Namely object equality and hash codes
#Autumn Life Clock In Season#
Hello, I'm Xiaomi. Welcome to my WeChat official account! Today, we will discuss a frequently asked question in Java development: "Why is rewritingEqualsMethod also needs to be rewrittenHashCodeMethod This question may appear frequently in your interviews, but it is not just an interview question, it also involves very important concepts in Java, Namely object equality and hash codes. Let's delve deeper into this issue and understand why it is so important.
equalandHashCode
In Java, each object has a defaultEqualsMethod, which compares whether the references of objects are equal, that is, comparing whether two objects are the same instance. However, in actual development, we usually need to compare the contents of objects for equality, rather than just comparing their references. That's why we need to rewriteEqualsThe reason for the method.
HashCode..Java...
Equals
By default, in Java, theEqualsThe method compares references to objects. If we don't rewriteEqualsMethod, then for two different objects, even if their contents are the same, callEqualsThe method will also returnFalseBecause their references are different.
Consider the following example:
In this example, althoughPerson1andPerson2The content of is the same, but they are different objects, soEqualsMethod returnFalse. This is obviously not the behavior we want.
To solve this problem, we need to rewriteEqualsMethod to compare the content of an object instead of a reference. Usually, we override in custom classesEqualsMethod to implement our own equality logic and compare whether the properties of an object are equal.
HashCode
Okay, now we know why we need to rewrite itEqualsMethod, but why does it still need to be rewrittenHashCodeWhat about the method? This is because when using hash data structures, such as hash tables, we want equal objects to have equal hash codes.
In Java, hash tables use hash codes to determine the location of stored objects. If two equal objects have different hash codes, they will be stored in different positions in the hash table, resulting in incorrect lookup of these objects.
Consider the following example:
In this example, althoughPerson3andPerson4The content of is the same, but due to their different hash codes,set.contains(Person4)returnFalse. This is because the hash table cannot locate correctlyPerson4.
To solve this problem, we need to ensure that the rewriteEqualsThe object of the method must also be overriddenHashCode ..
HashCode
So, how to rewrite correctlyHashCodeWhat about the method? Java forHashCode .
Here are some rewritesHashCodeRules for methods:
- If two objects pass throughEquals .
- HashCode .
- HashCode ..
- .
.
EqualsandHashCode
Let's take a look at an example of how to rewriteEqualsandHashCodeMethod:
In this example, we have rewritten theEqualsMethod, comparedNameandAgeWhether the attributes are equal and then overriddenHashCodeMethod, usingObjects. hash .
END
To ensure the equality of objects and the correctness of the hash table, we need to rewriteEqualsMethod is also overriddenHashCode ..
When you encounter this issue in an interview, don't forget to emphasize itEqualsandHashCodeConsistency and performance of methods, as well as adherence to rewritingHashCode ..
I hope this article can help you better understand why rewriting is necessaryEqualsMethod also needs to be rewrittenHashCode ..
Knowing what it is and why it is
Disclaimer: The content of this article is sourced from the internet. The copyright of the text, images, and other materials belongs to the original author. The platform reprints the materials for the purpose of conveying more information. The content of the article is for reference and learning only, and should not be used for commercial purposes. If it infringes on your legitimate rights and interests, please contact us promptly and we will handle it as soon as possible! We respect copyright and are committed to protecting it. Thank you for sharing.(Email:[email protected])
Mobile advertising space rental |
Tag: Interviewer Favorite Pitfall Why must rewrite hashCode when rewriting
It was stored in the Yangtze River and dried out! After Samsung's flash memory price reduction, it announced a 50% reduction in production
NextHas Li Ziqi, the former top performer, returned? Can it still lift the top flow storm?
Guess you like
- Detail
-
Why Did WeChat's "Shake" and "Drift Bottle" Disappear? Netizens Share: From Love, Opportunities to AnecdotesDetail
2024-11-02 10:12:09 1
-
Methane Hydrate: From High Hopes to Diminished Enthusiasm, Does It Have a Future?Detail
2024-11-01 20:50:31 1
- Detail
-
Microsoft Announces End of Support for Windows 10 on October 14, 2025, Strongly Recommends Migration to Windows 11Detail
2024-11-01 12:01:58 1
-
Unlocking Another Dimension of the World: From Urban Movement to Outdoor Nouveau RicheDetail
2024-11-01 11:41:06 21
- Detail
- Detail
-
Kuaishou Rural Ecology Conference Kicks Off, Joining Hands with New Farmers to Create a Better LifeDetail
2024-11-01 10:21:07 21
-
Didi Station Bus Empowers Passenger Transport Enterprises to Transform and Upgrade, Providing Convenient and Efficient Travel Services for PassengersDetail
2024-11-01 10:18:08 1
-
Amazon Battles Temu and SHEIN: Can Increased Essentials Sales Revitalize the Giant?Detail
2024-11-01 10:04:45 1
- Detail
-
Returning to Natural Interaction: Hanwang Technology's MOUNTAIN Series Handwritten Notebook Ushers in a New Era of "Native Office"Detail
2024-10-31 15:33:14 1
-
Vtrip Technology Completes Hundreds of Millions of Yuan Series A Financing, Accelerating the Application of VtripGPT Large ModelDetail
2024-10-30 09:40:59 1
-
Wang Haoze, a 90s girl from Hebei: From Rocket Designer to Spaceflight Engineer, She Will Fly to the StarsDetail
2024-10-29 21:37:42 1
-
From "Underachiever" to Global Robotics Leader: The Legendary Rise of Wang XingxingDetail
2024-10-29 20:27:12 1
-
Tmall's Interconnection with WeChat: Unveiling the Secret to 800 Billion Yuan GMV Growth PotentialDetail
2024-10-29 17:53:43 1
-
The State of Hearing Loss in China's Elderly Population Cannot Be Ignored: 120 Million Affected, Online Fitting Becoming a New TrendDetail
2024-10-29 15:39:00 1
- Detail
-
The "Treasure Box" of Open Intelligent Document Processing from Hohos: Empowering Developers to Achieve Personalized and High-Efficiency Document Application DevelopmentDetail
2024-10-28 21:04:58 11