wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Question on HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on HashSet" Watch "Question on HashSet" New topic
Author

Question on HashSet

Amit Mukherjee
Greenhorn

Joined: Sep 10, 2009
Posts: 4
Hi,

Could someone please explain the following to me?

The output is:

3
2
1
0
4
3
2
1
0
4

I expected the output to be just one set of values :
3
2
1
0
4

Tha javadoc says: If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets.

Then why are the values coming twice? Has it got something to do with the hashcode of the objects?

Thanks.
James Ward
Ranch Hand

Joined: Apr 27, 2003
Posts: 263
You are indeed adding the values to hashset twice!

# fill(a); //once here
# a.addAll(a); //again here

So, the result is correct and expected.
Amit Mukherjee
Greenhorn

Joined: Sep 10, 2009
Posts: 4
James Ward wrote:You are indeed adding the values to hashset twice!

# fill(a); //once here
# a.addAll(a); //again here

So, the result is correct and expected.


Thanks James. But what I intended was that even if I add twice, HashSet should store it only once as duplicate values are not allowed in HashSet.

For e.g. If I do this:
fill(a);
fill(a);
a.add("1");

the result is still:
3
2
1
0
4

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
I tried it and got
campbell@queeg:~/java$ java TestSet
3
2
1
0
4
campbell@queeg:~/java$ javac TestSet.java
Dhaval Hansaliya
Greenhorn

Joined: Sep 14, 2009
Posts: 8
Hi Amit,

i have executed your code on my machine. it gives below output:

3
2
1
0
4

as expected.

thanks,
Amit Mukherjee
Greenhorn

Joined: Sep 10, 2009
Posts: 4
Now I am doubly confused!

Someone please help!! Why am I getting a different output? I have java version "1.6.0_13"
James Ward
Ranch Hand

Joined: Apr 27, 2003
Posts: 263
From JavaDocs (http://java.sun.com/j2se/1.4.2/docs/api/java/util/AbstractCollection.html#addAll(java.util.Collection))

Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)

That explains it?
Amit Mukherjee
Greenhorn

Joined: Sep 10, 2009
Posts: 4
Ahh..that might explain. Thanks a lot James.

What's surprising is the output changes with java versions. So with java version "1.4.2_14" the output is
3
2
0
4
1

whereas with java version 1.5 and higher, the output is:

3
2
1
0
4
3
2
1
0
4

Is this a bug with java 1.5 & higher versions?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Amit Mukherjee wrote:
Is this a bug with java 1.5 & higher versions?


Well, "bug" implies that it is not working as specified. If the documentation states that the result is undefined, then how can it be considered a "bug"?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
I got only one lot of output with Java6.

I went into src.zip and the addAll method appears to be un-overridden from AbstractCollection, and here it is:So I looked up add in HashSet and found this:Can't see how you manage to double the contents of a Set like that. I altered the original class like thisand got this output
campbell@queeg:~/java$ java TestSet
Type = java.lang.String, hash code = 30, value = 0
Type = java.lang.String, hash code = 31, value = 1
Type = java.lang.String, hash code = 32, value = 2
Type = java.lang.String, hash code = 33, value = 3
Type = java.lang.String, hash code = 34, value = 4
Not adding elements
3
2
1
0
4
campbell@queeg:~/java$
That is behaving as I would expect, using Java6u14.
Maybe somebody who is getting different outputs would like to try those alterations.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on HashSet