mutable fields for objects in a Java Set

后端 未结 4 1706
悲&欢浪女
悲&欢浪女 2020-12-16 18:18

Am I correct in assuming that if you have an object that is contained inside a Java Set<> (or as a key in a Map<> for that matter), any fields that are used to determi

相关标签:
4条回答
  • 2020-12-16 18:27

    That is correct, it can cause some problems locating the map entry. Officially the behavior is undefined, so if you add it to a hashset or as a key in a hashmap, you should not be changing it.

    0 讨论(0)
  • 2020-12-16 18:28

    The javadoc for Set says

    Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.

    This simply means you can use mutable objects in a set, and even change them. You just should make sure the change doesn't impact the way the Set finds the items. For HashSet, that would require not changing the fields used for calculating hashCode().

    0 讨论(0)
  • 2020-12-16 18:33

    Yes, that will cause bad things to happen.

    // Given that the Toy class has a mutable field called 'name' which is used
    // in equals() and hashCode():
    Set<Toy> toys = new HashSet<Toy>();
    Toy toy = new Toy("Fire engine", ToyType.WHEELED_VEHICLE, Color.RED);
    toys.add(toy);
    System.out.println(toys.contains(toy)); // true
    toy.setName("Fast truck");
    System.out.println(toys.contains(toy)); // false
    
    0 讨论(0)
  • 2020-12-16 18:45

    In a HashSet/HashMap, you could mutate a contained object to change the results of compareTo() operation -- relative comparison isn't used to locate objects. But it'd be fatal inside a TreeSet/TreeMap.

    You can also mutate objects that are inside an IdentityHashMap -- nothing other than object identity is used to locate contents.

    Even though you can do these things with these qualifications, they make your code more fragile. What if someone wants to change to a TreeSet later, or add that mutable field to the hashCode/equality test?

    0 讨论(0)
提交回复
热议问题