This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashSet question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashSet question" Watch "HashSet question" New topic
Author

HashSet question

Wong Martin
Greenhorn

Joined: Jan 14, 2011
Posts: 25
Hi everyone, I need help...

Source: from my instructor (I've finished the course...), though I strongly believe that it actually came from Internet...



This code normally printed out 2 1 0 as a result of line 14 wasn't involved... so to me this is quite obvious that nothing remain on HashSet a since a.remove(i2) in line

15 executed. I hope that I didn't get thinking in a wrong way...

Unfortunately, when line 14 executed, I could hardly understand that... it printed out 2 1 1 i2 in HashSet a: 46??? That means a.remove(i2) actually removed the i2 Object

in line 14??? And moreover, let's say if I just changed that:

line 14: i2 = 47;
line 15: //a.remove(i2);

It still print out the result 2 1 1 i2 inHasSet a: 46???

Can anyone tell how it works? Thank you!! ~
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

The set contains the values 45 and 46. Then you print the size. When you remove i1, you tell it to remove the value 45. Then, when remove i2, you tell it to remove the value 46.

Now, what happens when you add the line i2 = 47? The set contains the value 46, and you tell it to remove the value 47, which isn't contained by the set. So the resulting set should still contain the value 46.
Wong Martin
Greenhorn

Joined: Jan 14, 2011
Posts: 25
Thanks for your reply Stephan! But I still didn't get it..

The set still contains the value 46 instead of 47 after adding line 14, that means 47 wasn't contained by the set, so how come it could be removed if it wasn't contained?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

The remove method will remove an element if it exists. If the element isn't contained by the set, nothing happens.
Wong Martin
Greenhorn

Joined: Jan 14, 2011
Posts: 25
Understood ~ Thanks a lot ~
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47



Hello there, i slightly modified your code . if you observe i added an extra println statement after the statement "i2=47;"
This new print statement will print out 47, and the elements in the set will be 45 & 46.

so, when we say s.remove(i2), are we removing the i2's value(47) which is just assigned a value but not added into the collection ?

Thanks
Wong Martin
Greenhorn

Joined: Jan 14, 2011
Posts: 25
I did have the same question as yours when I read this code, and this should refer to the concept of "Stack and Heap storage" (even I am not sure...)


This is my thought so far.. Hope that anyone can correct my assumption if there is any mistake??
Francois Descamps
Greenhorn

Joined: Feb 14, 2011
Posts: 4

Hello,

To my mind, i understand the code like this (I might be wrong too) - I apologize by advance if i'm wrong :

// 1) Here we declare i2, a reference to an Integer Object,
// 2) We create an object (Integer) with the value 46 (It's the boxing way, it means =46 it's the same as new Integer(46))
// 3) We affect the reference of our object (Integer with value 46) to i2


// 4) We add the Reference (not the value) in the HashSet


// 5) We create an object (Integer) with the value 47 (It's the boxing way, it means =47 it's the same as new Integer(47))
// 6) We affect the reference of our object (Integer with value 47) to i2

// Note : Here we still have a reference to the Integer 46 in the set so no Garbage Collector, isn't it ?

// 7) We try to remove in s the same reference as i2 contained
// But in s we don't have in the set a reference to the integer object 47 so nothing it's removed


// 8) Here we create an integer object with the 46 value
// 9) here it's possible to remove the 46 value because Integer as the equals method so the contains method return true

// OR
// 10) It's the same "effect" as the point 8 and 9
// We create an object (Integer) with the value 46 (It's the boxing way, it means =46 it's the same as new Integer(46))
// We affect the reference of our object (Integer with value 46) to i2

// 11) We try to remove in s the same reference as i2 contained
// But it's possible with the equals method


I don't know if I'm enough clear.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

In Java, variables are always passed to methods by value.

When you say a.remove(i2), you pass the *value* of i2 to the remove method. What is the value of i2? Is it 46? No. It's a reference to an Integer which has the value 46.
When we change i2 like this: i2 = 47; what we really do is we create a new instance of Integer, with the value 47, and we assign a reference to that object to i2.

Let's call remove. When you call remove, you pass the reference to the Integer to it. The method will then call the Integer's hashCode() method to determine where in the HashSet it is located. It will see that 47 does not map to a bucket in the set, so nothing happens.
 
Consider Paul's rocket mass heater.
 
subject: HashSet question
 
Similar Threads
generics question (sample mocks)
Doubt in function remove in Set
ArrayListMagnet- problem compiling
Confusion on HashSet