Learn how to understand the equals and hashcode methods in Java and distinguish between the same and equal concepts of objects.

Source: Internet
Author: User

 

Http://tieba.baidu.com/F? Kz= 879340761

 

In some cases, we need to determine whether two objects are equal. Every class in Java inherits from the object class. It uses the equals () and hashcode () methods to determine whether two objects are equal.

1. Equals ()
Requirements:
1 Introspection: returns true for any non-null reference X, X. Equals (X;
2 reflection: For any non-null reference of X and Y, X. Equals (y) returns true only when Y. Equals (x) returns true;
3 Transfer: For any non-null reference X, Y, and Z, if X. equals (Y) is true, and Y. if equals (z) is true, X. equals (z) should return true;
4. Stability: For any non-null reference of X and Y, if the information used for comparison is not changed, true or false will be returned continuously no matter how many times X. Equals (Y) is called;
5. For any non-null reference X, X. Equals (null) should return false.

The default implementation of an object is to return true, that is, return x = Y, as long as the two objects have equal references;

If you want to overwrite (override) This method, you need to overwrite hascode () at the same time. The requirement is that two equal objects must have the same hash code.

2. hashcode ()
The following conventions must be observed:
1 If the object equals, The hashcode must be equal;
2. If equals () returns false, the hashcode () of the two objects may be the same. However, different int values returned by two objects can improve the running efficiency of HashTables.

As a common sense, the hascode () of an unequal object may return different int values.

3. Rules for equality of Objects
1. Determine whether the hashcode of the two objects is equal. If the two objects are not equal, the two objects are considered not equal.
2. If they are equal, call the equals method.

4. What is the greatest use of hashcode?
Collection in Java has two types: List and set.
Do you know the differences between them? The elements in the former set are ordered, and the elements can be repeated. The latter elements are unordered, but the elements cannot be repeated.
So here is a serious problem: to ensure that the elements are not repeated, what is the basis for determining whether the two elements are repeated?
This is the object. Equals method. However, if each added element is checked once, when there are many elements, the number of times that the elements added to the set are compared is very large.
Java uses the hash table principle. The hash algorithm is also called the hash algorithm, which directly specifies the data to an address based on the specific algorithm. as a beginner, The hashcode method actually returns the physical address of the Object Storage (which may not actually be ).
With hashcode, when a set needs to add a new element, it first calls the hashcode method of this element to locate the physical location where it should be placed.
If there are no elements in this position, it can be directly stored in this position without any comparison. If there are already elements in this position,
You can call its equals method to compare it with the new element. If it is the same, it will not be saved. If it is different, other addresses will be hashed.
Therefore, there is a conflict resolution problem. In this way, the actual number of calls to the equals method is greatly reduced, almost only one or two.

5. Why do we need to rewrite the hashcode and equals methods in hiberna?
In hibernate, the Set set is often used to save related objects, while the Set set cannot be repeated. So, the same principle is 4.

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.