EqualsHashCodeComparable.docx

advertisement
Equals/Hashcode
Very often you have a object you've created that you want to override the behavior of the .equals
method for. All objects have the methods equals and hashCode (they inherit them from Object).
But the default built in behavior is often not what you want. You might want to use this object as a key
in a HashMap or uses the contains function in an ArrayList. Here's what you need to know 1. The equals method returns true if two objects are "equal", where what it means to be equal is
up to you. By default 2 objects are only equal if they are pointers to the same object. The
equals method is implicitly called by lots of java library classes like ArrayList and Set.
2. If you override equals, you must override hashCode(). You probably don't want to override
hashCode. But you have to because if you don't your hashCode function will start breaking rule
4.
3. hashCode returns and integer based on the data within an object
4. If object1.equals(object2), their hashCodes must be the same
5. If object1 does not equal object2, their hashCode ought to be different but it's not a hard
requirement.
When you actually get to implementing equals, there's some boilerplate code you want to include. It's
not super important you understand it 100% at this point:
public boolean equals(Object obj) {
if (obj == null)
return false; //make sure the object's not null
if (getClass() != obj.getClass())
return false; //make sure the object is the same class
//ok, now we can actually convert object to the right class
Person other = (Person) obj;
//now write the code that compares other and your object here
}
Comparable
Very often you have an object you've created that you want to be able to sort. You might want to call
Collections.sort or Arrays.sort on a list of that object for example. Or maybe you want to use some
efficient sorting-based data structures like TreeMap or TreeSet. Here's how:
1. Add a method "int compareTo(YourClass other)" to your class. This method lets one
object of YourClass compare itself with another.
2. If your object is less than the other object, return a negative value from compareTo. If your
object is greater than the other object, return a positive value from compareTo. if your object is
equal to the other object, return a 0 from compareTo.
3. Add the line "implements Comparable<YourClass>" to your class definition
// just an example class that uses equals hashCode and Comparable for your
// reference
public class Rectangle implements Comparable<Rectangle>{
private int myWidth;
private int myHeight;
public Rectangle(int width, int height) {
myWidth = width;
myHeight = height;
}
public int hashCode() {
int someInt = 31;
return someInt*myWidth + myHeight;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Rectangle other = (Rectangle) obj;
if (myHeight != other.myHeight)
return false;
if (myWidth != other.myWidth)
return false;
return true;
}
public int compareTo(Rectangle other) {
int myArea = myWidth*myHeight;
int otherArea = other.myWidth * other.myHeight;
if(myArea < otherArea)
return -1;
if(myArea > otherArea)
return 1;
return 0;
// I could also just say
// return myArea - otherArea
}
}
Download