• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Question on HashSet

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic