I must be missing something here… I have the following code and output. Can you see why Category categoryToBeDeleted is not being deleted from the category set of each book in result?
Thanks!!
Code:
List<Book> result = ... //get list from database
final Category categoryToBeDeleted = ... //get category from database
System.out.println("categoryToBeDeleted id: " + categoryToBeDeleted.getId() + " name: " + categoryToBeDeleted.getName());
for (Book book : result) {
System.out.println("before remove :");
for (Category category : book.getCategories()) {
System.out.println("category id: " + category.getId() + " name: " + category.getName() + " equals: " + category.equals(categoryToBeDeleted));
}
System.out.println("-----------------------");
book.getCategories().remove(categoryToBeDeleted);
System.out.println("after remove :");
for (Category category : book.getCategories()) {
System.out.println("category id: " + category.getId() + " name: " + category.getName() + " equals: " + category.equals(categoryToBeDeleted));
}
System.out.println("-----------------------");
}
Output:
categoryToBeDeleted id: 10 name: cosmetics
before remove :
category id: 10 name: cosmetics equals: true
category id: 1 name: cleaning equals: false
-----------------------
after remove :
category id: 10 name: cosmetics equals: true
category id: 1 name: cleaning equals: false
-----------------------
before remove :
category id: 9 name: junk-2 equals: false
category id: 10 name: cosmetics equals: true
-----------------------
after remove :
category id: 9 name: junk-2 equals: false
category id: 10 name: cosmetics equals: true
-----------------------
before remove :
category id: 6 name: knick-knacks equals: false
category id: 4 name: baby equals: false
category id: 9 name: junk-2 equals: false
category id: 10 name: cosmetics equals: true
-----------------------
after remove :
category id: 6 name: knick-knacks equals: false
category id: 4 name: baby equals: false
category id: 9 name: junk-2 equals: false
category id: 10 name: cosmetics equals: true
-----------------------
P.S. Category contains:
@Override
public boolean equals(Object obj) {
if (obj instanceof Category) {
Category thatCategory = (Category) obj;
return this.id.equals(thatCategory.id);
}
return false;
}
Javadoc
boolean remove(Object o)
Removes the specified element from this set if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)
Your set is actually a
HashSet. Category isn’t implementinghashCode. As a result, when you go to remove the object, the object identifier is used as the hash code, and for each different object, even if it’s semantically equivalent, it ends up using a different hash code, looking in the wrong place in the hash set and not finding the appropriate matching object.Add
to
Categoryand all should be well.The requirement to override
hashCodecan be read in the JavaDoc for java.lang.Object#equals:As this text implies, the JavaDoc for hashCode covers this in more detail.
Whilst the JavaDoc for
removecould perhaps be clearer on the subject, it only referencesequalsas part of the specification for which object it will remove – it doesn’t say thatequalsis the only thing it will use.Finally, if you’re using Eclipse, there’s a warning you can turn on which will warn you if you override
equalswithout overridinghashCodeor vice-versa.