Your compare()
method is not transitive. If A == B
and B == C
, then A
must be equal to C
.
Now consider this case:
For A
, B
, and C
, suppose the containsKey()
method return these results:
childMap.containsKey(A.getID())
returnstrue
childMap.containsKey(B.getID())
returnsfalse
childMap.containsKey(C.getID())
returnstrue
Also, consider orders for A.getId()
!= B.getId()
.
So,
A
andB
would return0
, as outerif
condition will befalse
=>A == B
B
andC
would return0
, as outerif
condition will befalse
=>B == C
But, A
and C
, could return -1
, or 1
, based on your test inside the if
block. So, A != C
. This violates the transitivity principle.
I think, you should add some condition inside your else
block, that performs check similar to how you do in if
block.