Override equals and hashcode Methods

Source: Internet
Author: User

If you write the equals method for a class, you should write the hashcode method at the same time.
If the hashcode method is not provided, the compiler does not report errors and may not encounter any problems.
Conflict-based hash tables (similar to Java. util. Set also use the same hash table) may cause unpredictable consequences,
It may take a lot of time, so you must develop this habit: If you write the equals method, you can also provide the corresponding hashcode method.

Equals ():
It is used to compare two objects and compare object content.
The comparison of object content is the true purpose of designing equals (). the Java language has the following requirements for equals (). These requirements are mandatory.

To be followed. Otherwise, you should not waste time:
* Symmetry: If X. Equals (y) returns "true", then Y. Equals (x) returns "true ".
* Reflex: X. Equals (x) must return "true ".
* Analogy: If X. Equals (y) returns "true" and Y. Equals (z) returns "true", then z. Equals (X)

It should also return "true ".
* Consistency: If X. Equals (y) returns "true", as long as the content of X and Y remains unchanged

X. Equals (y) returns "true ".
* In any case, X. Equals (null) always returns "false"; X. Equals (and X objects of different types) always returns

Is "false ".

Hashcode ():
This function returns an integer code used for hash operations.

The Code represented by the variable is mixed up. The former is not only a code, but also a function to find the object location in memory.

The value returned by hashcode () is used to classify the position of an object in a specific collection object. These objects are

Hashmap, hashtable, hashset, and so on. This function and the above equals () function must be designed by yourself

Assists hashmap, hashtable, and hashset in searching and locating a large number of objects they collect.

How do these collection objects work? Imagine that each meta-object hashcode is a box encoded according to the Encoding

Each metadata object is included in the corresponding box according to the Code provided by hashcode. All the boxes add up to one

Hashset, hashmap, or hashtable object. When we need to find a meta object, we should first look at its code, that is

The integer value returned by hashcode (), so that we can find the box where it is located, and then each metadata object is taken out in the box.

Compare them one by one with the objects we are looking for. If the content of the two objects is the same, our search will end. This operation

It requires two important information: The hashcode () of the object, and the comparison of the object content.

The relationship between the return value of hashcode () and equals () is as follows:

* If X. Equals (y) returns "true", the hashcode () of X and Y must be equal.
* If X. Equals (y) returns "false", the hashcode () of X and Y may be equal or different. Because no

The same object returns the same hash value, that is, a conflict.

The reason for these two rules is actually very simple. For hashset, hashset can have one or more

Multiple boxes can have one or more unique meta objects in the same box (the hashset must be unique

Object ). This example shows that a meta object can have the same hashcode as other meta objects. However

The object must be the same as the object with the same content. Therefore, these two rules must be established.

When an object type is used as a collection object meta object, this object should have its own processing equals (), And/

Or handle the design of hashcode (), and follow the two principles mentioned above. Equals () First, check whether null is the same

Type 1. The same type is checked to avoid the loss of exceptions such as classcastexception. Query null

Avoid exceptions such as nullpointerexception.

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.