Reputation: 2940
I have the following simple Rectangle class. If two rectangles have the same height and width, they are equal and have the same hashcode. I added a new rectangle to a hashSet.
Set<Rectangle> set = new HashSet<Rectangle>();
set.add(new Rectangle(3,3));
When I try to call contains on a new rectangle with same height and width, it returns false.
set.contains(new Rectangle(3,3))
returns false. I can't figure out why. Any ideas?
public class Rectangle implements Comparable<Rectangle> {
final int height, width, area, minimumEdge, maximumEdge;
public Rectangle(int height, int width) {
this.height = height;
this.width = width;
area = height * width;
maximumEdge = height > width ? height : width;
minimumEdge = height < width ? height : width;
}
public int compareTo(Rectangle rect2) {
if (rect2.minimumEdge > this.minimumEdge) {
return -1;
} else if (rect2.minimumEdge < this.minimumEdge) {
return 1;
} else {
return 0;
}
}
public int hashCode(){
return ((width + height)*31);
}
public boolean equals(Rectangle rect2){
return (this.height == rect2.height && this.width == rect2.width);
}
}
Upvotes: 3
Views: 2575
Reputation: 92
Here:
public boolean equals(Rectangle rect2){
return (this.height == rect2.height && this.width == rect2.width);
}
You make your own equals method, not override superclass method.
You must write like:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Rectangle rect2 = (Rectangle) obj;
return (this.height == rect2.height && this.width == rect2.width);
}
Upvotes: 3
Reputation: 888223
You haven't actually overridden equals()
.
You created a new equals(Rectangle)
method, which has nothing to do with the virtual equals(Object)
method.
This is why you should always add @Override
when trying to override methods.
Upvotes: 14