Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on HashSet

 
Amit Mukherjee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it and got
campbell@queeg:~/java$ java TestSet
3
2
1
0
4
campbell@queeg:~/java$ javac TestSet.java
 
Dhaval Hansaliya
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic