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())
returns true
childMap.containsKey(B.getID())
returns false
childMap.containsKey(C.getID())
returns true
Also, consider orders for A.getId()
!= B.getId()
.
So,
A
and B
would return 0
, as outer if
condition will be false
=> A == B
B
and C
would return 0
, as outer if
condition will be false
=> 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.