HashSet doesn't remove an object

Nacho

Probably I miss something using a HashSet and HashCode, but I don´t know why this doesn't work as I thought. I have an object with the HashCode is overridden. I added the object to a HashSet and later I change a property (which is used to calculate the HashCode) then I can´t remove the object.

public class Test 
{
    public string Code { get; set; }
    public override int GetHashCode()
    {
        return (Code==null)?0: Code.GetHashCode();
    }
}

public void TestFunction()
{
    var test = new Test();
    System.Collections.Generic.HashSet<Test> hashSet = new System.Collections.Generic.HashSet<Test>();
    hashSet.Add(test);
    test.Code = "Change";

    hashSet.Remove(test);  //this doesn´t remove from the hashset
}
Jon Skeet

Firstly, you're overriding GetHashCode but not overriding Equals. Don't do that. They should always be overridden at the same time, in a consistent manner.

Next, you're right that changing an objects hash code will affect finding it in any hash-based data structures. After all, the first part of checking for the presence of a key in a hash-based structure is to find candidate equal values by quickly finding all existing entries with the same hash code. There's no way that the data structure can "know" that the hash code has been changed and update its own representation.

The documentation makes this clear:

You can override GetHashCode for immutable reference types. In general, for mutable reference types, you should override GetHashCode only if:

  • You can compute the hash code from fields that are not mutable; or
  • You can ensure that the hash code of a mutable object does not change while the object is contained in a collection that relies on its hash code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java HashSet remove(e) method doesn't behave as I expected

From Dev

SetUniqueList, HashSet and Set don't remove duplicates from a List of an object

From Dev

Object doesn't support property or method 'remove'

From Dev

Why doesn't NSMutableSet remove an object contained in it?

From Dev

HashSet doesn't contain my custom object even though I override equals(Object o)

From Dev

HashSet doesn't contain my custom object even though I override equals(Object o)

From Dev

Why doesn't HashSet maintain uniqueness?

From Dev

ObservableCollection.Remove doesn't work, even if the object is in the list

From Dev

Html canvas - Object doesn't support property or method 'remove'

From Dev

Hibernate doesn't remove object from collection with children in specific case

From Dev

ObservableCollection.Remove doesn't work, even if the object is in the list

From Dev

Remove item from Object set that doesn't exist in query set

From Dev

destroying object doesn't remove delegate from invocation list

From Dev

can't convert hashset to treeset with object collection

From Dev

Why can't I write if (object is HashSet<>) but it's okay if I write (object.GetType() == typeof(HashSet<>))

From Dev

Using HashSet to remove items not in another collection based on a property of T

From Dev

How to make HashSet<T> invoking T.equals(Object ) when HashSet.add() is called

From Dev

Parallel stream from a HashSet doesn't run in parallel

From Dev

Attemping to add a value to a HashSet doesn't change the amount of values in it

From Dev

Why does HashMap have iter_mut() but HashSet doesn't?

From Dev

Attemping to add a value to a HashSet doesn't change the amount of values in it

From Dev

HashSet<HashSet<int>> doesn't allow duplicate in Java but it does in C#

From Dev

Allow_Remove doesn't remove the collection

From Dev

Why .remove() doesn't remove the element?

From Dev

removeview() doesn't remove android

From Dev

Doesn't remove from list

From Dev

FadeOut and Remove into a FOR doesn't work

From Dev

removeview() doesn't remove android

From Dev

RemoveAttr doesn't remove disabled

Related Related

  1. 1

    Java HashSet remove(e) method doesn't behave as I expected

  2. 2

    SetUniqueList, HashSet and Set don't remove duplicates from a List of an object

  3. 3

    Object doesn't support property or method 'remove'

  4. 4

    Why doesn't NSMutableSet remove an object contained in it?

  5. 5

    HashSet doesn't contain my custom object even though I override equals(Object o)

  6. 6

    HashSet doesn't contain my custom object even though I override equals(Object o)

  7. 7

    Why doesn't HashSet maintain uniqueness?

  8. 8

    ObservableCollection.Remove doesn't work, even if the object is in the list

  9. 9

    Html canvas - Object doesn't support property or method 'remove'

  10. 10

    Hibernate doesn't remove object from collection with children in specific case

  11. 11

    ObservableCollection.Remove doesn't work, even if the object is in the list

  12. 12

    Remove item from Object set that doesn't exist in query set

  13. 13

    destroying object doesn't remove delegate from invocation list

  14. 14

    can't convert hashset to treeset with object collection

  15. 15

    Why can't I write if (object is HashSet<>) but it's okay if I write (object.GetType() == typeof(HashSet<>))

  16. 16

    Using HashSet to remove items not in another collection based on a property of T

  17. 17

    How to make HashSet<T> invoking T.equals(Object ) when HashSet.add() is called

  18. 18

    Parallel stream from a HashSet doesn't run in parallel

  19. 19

    Attemping to add a value to a HashSet doesn't change the amount of values in it

  20. 20

    Why does HashMap have iter_mut() but HashSet doesn't?

  21. 21

    Attemping to add a value to a HashSet doesn't change the amount of values in it

  22. 22

    HashSet<HashSet<int>> doesn't allow duplicate in Java but it does in C#

  23. 23

    Allow_Remove doesn't remove the collection

  24. 24

    Why .remove() doesn't remove the element?

  25. 25

    removeview() doesn't remove android

  26. 26

    Doesn't remove from list

  27. 27

    FadeOut and Remove into a FOR doesn't work

  28. 28

    removeview() doesn't remove android

  29. 29

    RemoveAttr doesn't remove disabled

HotTag

Archive