Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.Set.remove(Object o) - problem

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

remove

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.)

like image 306
rapt Avatar asked Dec 02 '22 01:12

rapt


1 Answers

Your set is actually a HashSet. Category isn't implementing hashCode. 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

@Override
public int hashCode() {
    return id.hashCode();
}

to Category and all should be well.

The requirement to override hashCode can be read in the JavaDoc for java.lang.Object#equals:

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

As this text implies, the JavaDoc for hashCode covers this in more detail.

Whilst the JavaDoc for remove could perhaps be clearer on the subject, it only references equals as part of the specification for which object it will remove - it doesn't say that equals is 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 equals without overriding hashCode or vice-versa.

like image 154
Jon Bright Avatar answered Dec 04 '22 11:12

Jon Bright