Strange behavior of HashSet
I created set like this:
HashSet<Integer> col = new HashSet<Integer> ();
and add 12 elements:
col.add(1);
col.add(2);
col.add(3);
col.add(4);
col.add(5);
col.add(6);
col.add(7);
col.add(8);
col.add(9);
col.add(10);
col.add(11);
col.add(12);
when i print them on the screen, all elements are printed - that's ok. Problem is, when I started to debug (in eclipse), I was shocked, because in debug window I can't see all elements in HashSet col - I can see only 1,2,3,4, 9,10,11,12, numbers 5,6,7,8 "dissapeared" or what. Simply debug show there are only 8 element (not 12) in my collection...how is this possible? try it, you will see exactly the same. thx
[764 byte] By [
mila] at [2007-11-27 1:53:58]

# 2
what's probably happened is the "missing" elements are in different buckets ( which I won't go into here). dig a little deeper through the Set in the debugger and you'll find them. there's nothing wrong with HashSet!
the way the eclipse debugger shows HashTables and the like - which your HashSet uses internally - is often unintuitive to navigate. you get used to it. if you're looking at the HashMap$Entry elements in the debugger, expand them one at a time, you'll see the "missing" elements eventually
# 3
ok, I saw better and noticed, that some elements are in one level (8), and others (4) are nested:
in debug it it looks like:
- 12 (next = 5)
- 10 (next = 7)
- 3 (next=null)
- 1 (next=null)
- 11 (next=6)
- 9 (next=8)
- 4 (next=null)
- 2 (next=null)
I just can't understand why?
mila at 2007-7-12 1:24:59 >

# 5
> georgemc
> exactly!
>
> but question is why are elements saved in this way?
just the way the hashcodes worked out, I think. collections that deal with hashes don't just store data sequentially, they use hash codes to decide which bucket to keep a particular object in. that way, they can use the hash code to look objects in hash tables up quicker
</horrendously_oversimplified_answer>
# 7
ok, but why some elements have next element, some not, it looks like it's random...I would like to understand system. in docs is nothing about what i want to know
mila at 2007-7-12 1:24:59 >

# 9
> ok, but why some elements have next element, some
> not, it looks like it's random...I would like to
> understand system. in docs is nothing about what i
> want to know
because of how hashing works. follow stefans link for a better explanation than you'll get here. the bottom line really, though, is that you don't need to know. the only impact this has is on performance when you're dealing with very large collections, in which case you can optimize your hash code to make the collections more efficient
# 10
Well yes and no. You need to know for debugging. I came across this myself no more than a month ago. I was also debugging in Eclipse. I was trying to see if 2 Sets were equal and they looked equal. But once I saw some elements were hidden sort of, I could see that they were not equal.
So for functional purposes you don't need to know, but when debugging it helps.