HashSet.remove() and Iterator.remove() not working

前端 未结 10 1497
借酒劲吻你
借酒劲吻你 2020-12-13 00:32

I\'m having problems with Iterator.remove() called on a HashSet.

I\'ve a Set of time stamped objects. Before adding a new item to the Set, I loop through the set, i

10条回答
  •  無奈伤痛
    2020-12-13 00:45

    Under the covers, HashSet uses HashMap, which calls HashMap.removeEntryForKey(Object) when either HashSet.remove(Object) or Iterator.remove() is called. This method uses both hashCode() and equals() to validate that it is removing the proper object from the collection.

    If both Iterator.remove() and HashSet.remove(Object) are not working, then something is definitely wrong with your equals() or hashCode() methods. Posting the code for these would be helpful in diagnosis of your issue.

提交回复
热议问题