aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion on HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion on HashSet" Watch "Confusion on HashSet" New topic
Author

Confusion on HashSet

Anupam anand
Greenhorn

Joined: Jun 28, 2010
Posts: 7



I am confused how its output is 2 1 1. Please anyone explain it. Thanks in advanced.

[Edit: added code tags - MB]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

What would you expect the output to be?

You add two numbers. You add a duplicate, but this is a Set, so it doesn't allow duplicates. You remove one of them. You then try to remove a different value, so that has no effect.
ket bhav
Greenhorn

Joined: Aug 28, 2011
Posts: 22
Yes perfect explanation by Metthew Brown.

If you use List instead of set like


Then you will get 3 2 2... That might be your expectation...
Prince Sewani
Ranch Hand

Joined: Nov 24, 2010
Posts: 32
But Mathew,

I've a doubt,Set doesn't allows duplicates so the duplicate won't be added,it will be discarded rather,so when we remove i1,output is 1 that's fine and when we remove i2 as well,shouldn't the output be 0 in-spite of 1?

Matthew Brown wrote:What would you expect the output to be?

You add two numbers. You add a duplicate, but this is a Set, so it doesn't allow duplicates. You remove one of them. You then try to remove a different value, so that has no effect.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Prince Sewani wrote:But Mathew,
I've a doubt,Set doesn't allows duplicates so the duplicate won't be added,it will be discarded rather,so when we remove i1,output is 1 that's fine and when we remove i2 as well,shouldn't the output be 0 in-spite of 1?


The set does not contain 47. So the command "set.remove(47)" has no effect. The set is unchanged and its size is still 1.
Prince Sewani
Ranch Hand

Joined: Nov 24, 2010
Posts: 32
Hi Dennis,

But the set contains i2 i.e. 46 right??


Dennis Deems wrote:
Prince Sewani wrote:But Mathew,
I've a doubt,Set doesn't allows duplicates so the duplicate won't be added,it will be discarded rather,so when we remove i1,output is 1 that's fine and when we remove i2 as well,shouldn't the output be 0 in-spite of 1?


The set does not contain 47. So the command "set.remove(47)" has no effect. The set is unchanged and its size is still 1.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

Prince Sewani wrote:But the set contains i2 i.e. 46 right??

The set contains 46, which used to be the value of i2. But it isn't any more - you assigned i2 to a different object.
Prince Sewani
Ranch Hand

Joined: Nov 24, 2010
Posts: 32
Oh yeah,Sorry I missed that one out..

Matthew Brown wrote:
Prince Sewani wrote:But the set contains i2 i.e. 46 right??

The set contains 46, which used to be the value of i2. But it isn't any more - you assigned i2 to a different object.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Confusion on HashSet