You are on the right track, overriding hashcode
and equals
, except you are incorrectly overriding the equals
method from Object
in HashSquareSpec
(and SquareSpec
). The parameter must be an Object
. Because it's not overridden, equals
from Object
is called, which compares object references to see if they're the same object. They aren't, so the "duplicate" is allowed.
Try:
@Override
public boolean equals(Object other)
{
if(this.toString().equals(other.toString()))
return true;
else
return false;
}
You should also test if other
is null
and then ensure that other
is the same type.
Include the @Override
annotation so that the compiler will complain if the method doesn't actually override anything.