Search code examples
javaobjecthashtablecontainshashset

how to find and return objects in java hashset


According to the HashSet javadoc, HashSet.contains only returns a boolean. How can I "find" an object in a hashSet and modify it (it's not a primitive data type)?

I see that HashTable has a get() method, but I would prefer to use the set.


Solution

  • You can remove an element and add a different one.

    Modifying an object while it is in a hash set is a recipe for disaster (if the modification changes the hash value or equality behavior).