title: HashSet.remove() and Iterator.remove() not working
body: <p>I'm having problems with Iterator.remove() called on a HashSet.</p>
  
  <p>I've a Set of time stamped objects.  Before adding a new item to the Set, I loop through the set, identify an old version of that data object and remove it (before adding the new object).  the timestamp is included in hashCode and equals(), but not equalsData().</p>
  
  <pre><code>for (Iterator&lt;DataResult&gt; i = allResults.iterator(); i.hasNext();)
  {
      DataResult oldData = i.next();
      if (data.equalsData(oldData))
      {   
          i.remove();
          break;
      }
  }
  allResults.add(data)
  </code></pre>
  
  <p>The odd thing is that i.remove() silently fails (no exception) for some of the items in the set.  I've verified</p>
  
  <ul>
  <li><p>The line i.remove() is actually called.  I can call it from the debugger directly at the breakpoint in Eclipse and it still fails to change the state of Set</p></li>
  <li><p>DataResult is an immutable object so it can't have changed after being added to the set originally.</p></li>
  <li><p>The equals and hashCode() methods use @Override to ensure they are the correct methods.  Unit tests verify these work.  </p></li>
  <li><p>This also fails if I just use a for statement and Set.remove instead.  (e.g. loop through the items, find the item in the list, then call Set.remove(oldData) after the loop).</p></li>
  <li><p>I've tested in JDK 5 and JDK 6.</p></li>
  </ul>
  
  <p>I thought I must be missing something basic, but after spending some significant time on this my colleague and I are stumped.  Any suggestions for things to check?</p>
  
  <p>EDIT:</p>
  
  <p>There have been questions - is DataResult truly immutable.  Yes.  There are no setters.  And when the Date object is retrieved (which is a mutable object), it is done by creating a copy.</p>
  
  <pre><code>public Date getEntryTime()
  {
      return DateUtil.copyDate(entryTime);
  }
  
  public static Date copyDate(Date date)
  {
      return (date == null) ? null : new Date(date.getTime());
  }
  </code></pre>
  
  <p>FURTHER EDIT (some time later):
  For the record -- DataResult was not immutable!  It referenced an object which had a hashcode which changed when persisted to the database (bad practice, I know).  It turned out that if a DataResult was created with a transient subobject, and the subobject was persisted, the DataResult hashcode was changed.</p>
  
  <p>Very subtle -- I looked at this many times and didn't notice the lack of immutability.</p>