Understanding of Java:equals and Hashcode

Source: Internet
Author: User

We generally use equals to compare the contents of an object , whereas the Equals method in object is a reference to a comparison object , as follows:

public boolean equals (Object obj) {        return (this = = obj);    }
So when we want to compare the contents of an object, we need to rewrite the Equals method, assuming that the user class has a string name, an int age two field, which is automatically generated with eclipseThe equals method is spelled as follows:

public boolean equals (Object obj) {if (this = = obj) return true;//Both reference equality is the same object so the content is equal if (obj = null) return false;if (GetC Lass ()! = Obj.getclass ())  //The classes of both are unequal, then the contents cannot be equal to return false; User other = (user) obj;  Turn it down if (age = other.age)  //The following is the start comparison content return false;if (name = = null) {if (other.name! = null) return false;} else if (!name.equals (other.name)) return False;return true;}
A test case

public class Test {public static void main (string[] args) {User User1 = new User (); User User2 = new user (); System.out.println (User1.equals (user2)); }}
Print true. If you remove the Equals method overridden by the user class, then User1.equals () invokes the Equals method of the object class, comparing references instead of content, User1 and user2 are significantly different, so printing false. The common Equals method compares the object's contents, that is, the object belongs to the same class and the field properties are equal.

Hashcode is primarily used for class-set frameworks. Suppose we do not override the Hashcode method for a class, and the object calls the Hashcode method when it calls the Hashcode method of the object class:

public native int hashcode ();
The generated hash values are not the same, and if we override the Hashcode method, the same hash value can be generated for objects of the same content .

As for the Hashcode method, we generally do not need to remember that the direct use of tools to build just fine.

Test Case:

public class Test {public static void main (string[] args) {//TODO auto-generated method stub hashset<user> set = New Hashset<> ( ); User User1 = new User ("Leelit", 21); User User2 = New User ("Leelit", 21); User User3 = new user (); System.out.println (User1.hashcode ()); System.out.println (User2.hashcode ()); System.out.println (User3.hashcode ()); Set.add (user1); Add User1 and User2set.add (User2); System.out.println (Set.size ()); Hashmap<user, string> map = new hashmap<> (), Map.put (New User ("Lina", +), "value"); System.out.println (Map.get (New User ("Lina", 21));}}

Printing results:

-1106625705-11066257059611value

Remove the Hashcode method that is overridden by the user class to print:

311683221722537254336342null

Without overriding the Hashcode method: two objects, even if the content is equal, generate different hash values, so you can add into the set inside, we know that hashset can not have duplicate elements, so that is why you have to rewrite the hashcode reason The same as for HashMap although put and get are the same key, but because the hash value is different, and is considered as a different key, so can not be taken out.

After overriding the Hashcode method, two equal objects generate the same hash value.



Understanding of Java:equals and Hashcode

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.