The contract for equals(object) method specifies 4 properties to follow: Reflexive, Symmetric, Transitive and Consistent. While I understand the danger of not following Reflexive, Symmetric and Consistent , and can definitely agree its good to follow transitive, I was wondering what harm it would bring if its violating the Transitive property?
Specifically, which of the Java library (or various third party libraries) need the dependency upon equals to be transitive to work correctly? In my understanding, the Collections framework will work if the other 3 properties are well implemented.
Assume three objects a,b,c with
(Pseudocode,
x == ystands forx.equals(y)).Now, let’s add the objects to a set:
In contrast, if we were to add them in a different order:
That is clearly counter-intuitive and doesn’t match the behavior one would expect of a set. For example, it would mean that the union of two sets (i.e. the state of s after
s.addAll(someOtherSet)) could depend on the implementation (order of elements) of someOtherSet.