ArrayList removeAll() not removing objects
You have to know that
ArrayList#removeAll(Collection)
makes a call to
ArrayList#contains(Object)
which makes a call to
ArrayList#indexOf(Object)
which finally calls
Object#equals
So if equals
is not correctly overridden (following the equals
contract rules), you're not getting the correct behaviour.
How are 2 members determined to be equal? I'm guessing if they have the same ID, you deem them equal, however java wants them to be the exact same reference in memory which may not be the case. To correct for this you can override the equals
function to have it return if the ids are equal:
public class Member {
//..
@Override
public boolean equals(Object anObject) {
if (!(anObject instanceof Member)) {
return false;
}
Member otherMember = (Member)anObject;
return otherMember.getUserUID().equals(getUserUID());
}
}
Also when you override .equals
it is recommended to also override hashCode
so that the objects also work correctly in hashing functions like Set
or Map
.