0

I have a HashSet<User> and a User declared outside a while loop, and inside the loop I am setting the name with a setter method and then adding the User to the HashSet with add(User).

This seemed to only update the single User in the HashSet. Only when I declare the User within the while loop is a new User added every time to the HashSet.

I am thinking this is because if the HashSet add() method receives an object with the same memory address as an object currently in the set, it will only update the object and not add another new object to the HashSet. Is this correct?

Then instantiating the User within the loop will cause it to have a different memory address and thus be added to the HashSet as a new object.

I can put code on here, but this is for an assignment and I don't think my professor would like me to share code. The question I have is more about the Java language and not about my assignment, as you can see. If everyone absolutely needs to see code, however, I'll post something useful.

iagreen
  • 31,470
  • 8
  • 76
  • 90
Alex
  • 325
  • 7
  • 16
  • 3
    see my answer for this question: http://stackoverflow.com/q/29830784/217324, it seems to be the same issue. btw just because you can't post the real code that's giving you the problem doesn't mean you can't construct a sample that reproduces the specific issue. – Nathan Hughes Sep 28 '15 at 00:25
  • @nathanhughes well it clearly wasn't needed as I expected. If it were needed then I would have posted something promptly. – Alex Sep 29 '15 at 16:23

1 Answers1

0

I am thinking this is because if the HashSet add() method receives an object with the same memory address as an object currently in the set, it will only update the object and not add another new object to the HashSet. Is this correct?

That is correct. A HashSet by definition will allow for only one of each unique object. However, the part about updating the object is irrelevant to its membership in the HashSet. Since the HashSet contains only references, any changes you make to the objects (in or out of your while loop) will reflect in the HashSet automatically.

Then instantiating the User within the loop will cause it to have a different memory address

Also true. You'll have different references per object for each instantiation. The HashSet will store one and only one of each.

ThisClark
  • 14,352
  • 10
  • 69
  • 100
  • Ahh OK so the actual hashing involves the memory address as input? – Alex Sep 28 '15 at 00:26
  • I'm not certain about the hashing algorithm. You could find the source in your JDK installation files. I also found it in [this answer](http://stackoverflow.com/questions/9364134/what-hashing-function-does-java-use-to-implement-hashtable-class#answer-9364329). If you're wondering why I linked to a HashMap algorithm, the [docs](http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html) state *This class implements the Set interface, backed by a hash table (actually a HashMap instance).* – ThisClark Sep 28 '15 at 00:32