In my java program, I have a map with a correlation between someID and data object of type MyClass.
ConcurrentMap<Integer, MyClass> a;
Less often but regularly, I have to check whether one of the "value" objects contain a certain long as variables b. Since I don't know the correlating someId to my otherId, I cannot access it via the map. To do so I would like to use containsValue:
a.containsValue(otherId)
To accomplish that I overwrote the hashCode and the equals function.
@Override
public boolean equals(Object o) {
if ((o instanceof MyClass) && (((MyClass) o).someId().equals(this.someId()))) {
return true;
} else
if ((o instanceof Long) && ((Long) o) == this.otherId) {
return true;
} else {
return false;
}
}
NOTE: The otherId in the code does not necessarily need to be the someID of the map.
The first if statement is symmetric, the second obviously not. I do understand that this is not nice, but writing a loop every time is not nice either. In this post they say a asymmetric equals will be ignored. How can I make java ignore the asymmetry problem?