Java equals () and hashcode () rewrite summary

Source: Internet
Author: User

In real-world development, you sometimes encounter scenarios where you need to compare different instance objects of the same class, and generally inherit from the Equals () and hashcode () of the object parent to meet the requirements, but not all of the scenarios. For example, if you only need to use a few object properties to determine if the comparison is the same object, then we need to customize the Equals () and hashcode () implementations to override the methods in object.

1. Equals () method override Considerations
A. Reflexivity: For any reference value x,x.equals (x) must be true.
B. Symmetry: for arbitrary reference values x and Y, when X.equals (y) returns True, Y.equals (x) must also return true.
C. transitivity: For arbitrary reference values x, Y, and Z, if X.equals (y) returns True,
and Y.equals (Z) also returns True, then X.equals (z) must also return true.
D. Consistency: for arbitrary reference values x and Y, if the object information used for the equals comparison has not been repaired
Change, multiple calls to X.equals (Y) either return true consistently, or return false consistently.
E. Non-nullability: x,x.equals (NULL) must return FALSE for any non-null reference value.

2. Rewrite the Equals () method and be sure to override the Hashcode () method
Hashcode is a fast access for hashing data, such as using the Hashset/hashmap/hashtable class to store data, based on the hashcode value of the stored object to determine whether the same.
If you rewrite equals () without overriding the Hashcode () method, it can cause confusion between objects.
The relationship between the Equals () and Hashcode () methods is satisfied:
(1) when Obj1.equals (OBJ2) is true, obj1.hashcode () = = Obj2.hashcode () must be true
(2) when obj1.hashcode () = = Obj2.hashcode () is False, Obj1.equals (OBJ2) must be false
3. Sample Code

Package Com.vdlm.dal.model; Public classUserviewscore {PrivateInteger Totalscore;PrivateString Taskcontentid;PrivateString userId;PrivateString type; PublicStringGettaskcontentid() {returnTaskcontentid; } Public void Settaskcontentid(String Taskcontentid) { This. Taskcontentid = Taskcontentid; } PublicStringgetUserId() {returnUserId; } Public void Setuserid(String userId) { This. UserID = userid; } PublicStringGetType() {returnType } Public void SetType(String type) { This. type = type; } PublicBooleanequals(ObjectObject) {if( This==Object) {return true; }if(Object==NULL) {return false; }if(GetClass ()! =Object. GetClass ()) {return false; } Userviewscore Userviewscore = (userviewscore)Object;if(Userviewscore.getuserid () = =NULL|| Userviewscore.gettaskcontentid () = =NULL) {return false; }returnUserid.equals (Userviewscore.getuserid ()) && taskcontentid.equals (Userviewscore.gettaskcontentid ()); } Public int hashcode(){intresult = Userid.hashcode () +taskcontentid.hashcode ();returnResult } PublicIntegerGettotalscore() {returnTotalscore; } Public void Settotalscore(Integer Totalscore) { This. Totalscore = Totalscore; }}

Unit tests:

public class Userviewscoretest {@Test public void Test () {Userviewscore user1 = new Userviewscore ();User1. Setuserid("111");User1. Settaskcontentid("123");Userviewscore user2 = new Userviewscore ();User2. Setuserid("111");User2. Settaskcontentid("123");Userviewscore User3 = new Userviewscore ();User3. Setuserid("111");User3. Settaskcontentid("123");To verify the symmetry of overriding equals system. out. println("User1.equals (user2):"+user1. Equals(User2));System. out. println("User2.equals (user1):"+user2. Equals(user1));Verify that the transitive system overrides equals. out. println("User1.equals (user2):"+user1. Equals(User2));System. out. println("User2.equals (user3):"+user2. Equals(User3));System. out. println("User1.equals (user3):"+user1. Equals(User3));Verifying the reflexive system overriding equals. out. println("User1.equals (user1):"+user1. Equals(user1));Verify that the non-nullability system overriding equals is. out. println("user1.equals (NULL):"+user1. Equals(null));}}

Java equals () and hashcode () rewrite summary

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.