Why does removeAll(Collection<?> a)
returns true when the object inside the collection was not removed? I overrid the hashcode and equals method for my custom object. The hashcode of the Object from the input is the same as the hashcode of the object inside the selection.
private void onRemove(IStructuredSelection selection) {
boolean removeAll = getInput().removeAll(selection.toList()); // returns true
Set<ReadingNodeCfg> input = getInput(); // Object from selection is still there
}
The object from the input is the same as the object in the selection, so why wont he delete that?
Best regards
edit1 : i generated equals and hashcode by eclipse, getInput() returns a HashSet<ReadingNodeCfg>
, selection.toList() returns a List<ReadingNodeCfg>
edit2 :
for (Object object : selection.toList()) {
boolean remove = getInput().remove(object); // returns false
int hashCode = object.hashCode(); // returns 1130504316
int hashCode2 = getInput().iterator().next().hashCode(); // returns 1130504316
boolean equals = object.equals(getInput().iterator().next()); // returns true
}
edit3 : I am using now an IObservableList. That works fine now!
The hashcode changed in the input after modifying. And the object i wanted to delete had the same hashcode as the modified input object, but anyway it didnt worked and im still not sure why.
SOLUTION:
Well i solved the problem now. I created an example for any other people who got the same issues.
@Test
public void derp() {
Person person = new Person();
person.name = "jay";
Set<Person> humans = new HashSet<>();
humans.add(person);
person.name = "fred";
assertTrue(!humans.remove(person));
}
Its impossible to remove this person
object after it was modified. The hashset cant find the hashcode of the object person
in itself.
person
: 1337 Best regards
Why does removeAll(Collection a) returns true when the object inside the collection was not removed?
It only returns true when the collection is modified. If it appears your collection is not modified, you are looking the wrong collections. Instead of modifying the result of a getter (which is bad practice) I suggest passing the task to the caller. eg
boolean removeAll = removeAllFromInput(selection.toList()); // returns true
My bet is that getInput()
takes a defensive copy of the collection to avoid you incorrectly trying to change it.
SOLUTION:
Well i solved the problem now. I created an example for any other people who got the same issues.
@Test
public void derp() {
Person person = new Person();
person.name = "jay";
Set<Person> humans = new HashSet<>();
humans.add(person);
person.name = "fred";
assertTrue(!humans.remove(person));
}
Its impossible to remove this person object after it was modified. The hashset cant find the hashcode of the object person in itself.
Best regards
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.