I have classes like below
class A {
@Override
public boolean equals(Object other) { return true }
}
Class B extends A {
}
Class C extends A {
@Override
public boolean equals(Object other) { if ((other != null) || (other instanceOf B)) return false; }
}
In my main() I have this following code
Set<A> mySet = new CopyOnWriteArraySet<A>();
mySet.add(C);
I want mySet to contain C at this point
mySet.add(B); // #1
I want mySet to contain C & B at this point
mySet.remove(B); // #2
I want mySet to contain C at this point
I want to create a global queue where if object C exists in the queue B should be allowed to be added but not the other way around. So before #1 I am looping through the elements inside the set doing element.equals(B) with add on false.
But 1 is calling B.equals(C) which is returning true and so mySet has only one C object after this line
2 is again calling B.equals(C) which is returning true and removing the existing object C. Shouldn't it be C.equals(B) in this case? I am expecting this line as no-action
Is this some wrong use of CopyOnWriteArraySet?
Thanks for looking
equals
method is implemented incorrectly, so all sorts of things in the Collections API which rely onequals
are going to behave unexpectedly. – SemiologyObject.equals(null)
should always returnfalse
. – Wivern