This is sort of a duplicate of this question, however I'm looking for a bit more ELI5 explanation of the "mutable keys" and "buckets" in the answers.
Here is my code that I'm having trouble understanding:
HashSet<Object> set = new HashSet<Object>();
set.add(1); set.add(2); set.add(3);
for(Object i : set)
if(i.equals(1)) {
i = 1337;
System.out.println("FOUND");
}
for(Object i : set) System.out.println(i);
output:
FOUND
1
2
3
Why does this not print out 1337, 2, 3 instead?
Same question goes for removing objects.
EDIT:
This does what I want but I'm fairly certain its not the correct way to go about it:
for(Object i : set)
if(i.equals(1)) {
set.remove(i);
set.add(1337);
break;
}