Java equals () and hashcode () rewrite summary

Source: Internet
Author: User

  

In real-world development, you may 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 class to meet the requirements, but not all scenarios, such as simply using a few object attributes to determine If the comparison is the same object, then we need to customize the Equals () and hashcode () implementations to override the Square method 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. Guangzhou Java course rewrite the Equals () method and be sure to override the Hashcode () method

Hashcode is a fast access for hashing data, such as when using the Hashset/hashmap/hashtable class to store data, is based on the storage
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 class Userviewscore {

Private Integer Totalscore;

Private String Taskcontentid;

Private String userId;

Private String type;

Public String Gettaskcontentid () {

return taskcontentid;

}

public void Settaskcontentid (String taskcontentid) {

This.taskcontentid = Taskcontentid;

}

Public String getUserId () {

return userId;

}

public void Setuserid (String userId) {

This.userid = userId;

}

Public String GetType () {

return type;

}

public void SetType (String type) {

This.type = type;

}

public Boolean equals (Object object) {

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;

}

Return Userid.equals (Userviewscore.getuserid ()) && taskcontentid.equals
(Userviewscore.gettaskcontentid ());

}

public int hashcode () {

int result = Userid.hashcode () +taskcontentid.hashcode ();

return result;

}

Public Integer Gettotalscore () {

return totalscore;

}

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");

Verifying the symmetry of overriding equals

System.out.println ("User1.equals (user2):" +user1.equals (User2));

System.out.println ("User2.equals (user1):" +user2.equals (user1));

Verify the transitivity of overriding equals

System.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));

Validation overrides the reflexivity of equals

System.out.println ("User1.equals (user1):" +user1.equals (user1));

Verifying the non-nullability of overriding equals

System.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.