I have a class as follows:
public class Data{
int x;
ArrayList<Double> list;
}
Now, I want to write unit tests and compare this class with another one merely to check equality. However, I want to allow some room for error so that even if the Doubles are close with respect to some epsilon they're considered equal. Now, if I override the equals() method NetBeans and Sonar prompt me to override the hashCode() method as well which doesn't make any sense. The reason is that it's not feasible to simply implement a hashCode() method that outputs the same hash code value for CLOSE lists.
My question is this:
Should I continue with overriding the equals() method and just override hashCode() for the sake of passing Sonar check? (A dummy implementation for hashCode())
OR
Should I just implement this method to check closeness in my unit tests and not in the actual source code?
Set
ofData
– Scary Wombat Sep 12 at 0:26b.equals(c)
, it is still possible that!a.equals(c)
. That is, you've just created a version of equals that is not transitive. – another-dave Sep 12 at 0:42a.equals(b)
thena.hashCode() == b.hashCode()
, otherwise hash sets/maps do not work. You can trivially satisfy this by having (say)return 1
as your implementation, though I'm not claiming that's a good hash algorithm. But if you don't ever use hashed collections … ! Generally I find it morally abhorrent to write lousy code to satisfy style checkers, but there is a contract here that needs to be satisfied. – another-dave Sep 12 at 0:46