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.