Saturday, February 23, 2013

Java :: How to override equals

How to override equals

All what you need to do is add the following piece if code in your object:


@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != getClass())
return false;
YourObject rhs = (YourObject ) obj;
if (!rhs.getId().equals(getId())) // For Example
return false;
return true;
}


Have a nice day.

No comments:

Post a Comment