Note that there are some explanatory texts on larger screens.

plurals
  1. POCollection contains object but cannot remove it
    primarykey
    data
    text
    <p>I have a class that holds several collections and I'm having problems removing some of the objects from one of the collections. If I call collection.contains(object) it returns true then on the next line I call collection.remove(object) and the object doesn't get removed. </p> <p>This is the original code that didn't work. All of the collections are SortedSets. What confused me here was that the male collection is populated directly from the people collection, but when you try to remove the male objects from the people collections not all of them will be removed. </p> <pre><code> for(Person person : peopleBin.getPeople()) { if(person.isMale()) { peopleBin.getMen().add(person); } } peopleBin.getPeople().removeAll(peopleBin.getMen()); </code></pre> <p>Person has an equals method like this</p> <pre><code>public boolean equals( Object obj ) { if ( obj == null ) return false; if ( !(obj instanceof Person) ) return false; Person that = (Person)obj; return that.age == age &amp;&amp; that.id == id &amp;&amp; that.someCount == someCount ; } </code></pre> <p>Now when I replaced the first snippet's removeAll line with this I get strange behavior.</p> <pre><code> for(Person person: personBin.getMen()) { if(personBin.getPeople().contains(person)) personBin.getPeople().remove(person); } </code></pre> <p>if(personBin.getPeople().contains(person)) always returns true, but personBin.getPeople().remove(person) does not always remove. Sometimes it does and sometimes it doesn't.</p> <p>I have changed all of the class names and field names to be generic to post in a public forum.</p> <p>Any help would be greatly appreciated!</p> <p>edit: here is the compareTo impl</p> <pre><code> public int compareTo (Object o) { if ( ! ( o instanceof Person) ) { throw new ClassCastException(); } Person that = (Person)o; int comparison = 0; return ( (comparison = this.age () - that.age ()) != 0 ? comparison : ( (comparison = this.id - that.id) != 0 ? comparison : ( (comparison = this.someCount - that.someCount )))); } </code></pre> <p>edit: here is the hashCode impl</p> <pre><code>public int hashCode() { int result = 31; result = 61*result + age; result = 61*result + id; result = 61*result + someCount; return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload