I have a class
class Pair<T>{
private T data;
private T alternative;
}
Two pair objects would be equal if
this.data.equals(that.data) && this.alternative.equals(that.alternative) ||
this.data.equals(that.alternative) && this.alternative.equals(that.data)
I’m having difficulty correctly implementing the hashCode() part though. Any suggestions would be appreciated
This should do the trick:
Since you want to include both fields into the equals, you need to include both fields into the
hashCodemethod. It is correct if unequal objects end up having the same hash code, but equal objects according to your scheme will always end up having the same hash code with this method.