jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Get elements from HashMap 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 "Get elements from HashMap" Watch "Get elements from HashMap" New topic
Author

Get elements from HashMap

Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109
I need some help in understanding the following example from K and B book about getting elements from HashMap

The example is from Chap 7 page 565

Simplified version of the code is below

import java.util.*;
Class Dog {
public Dog(String n)
{
name =n;
}
String name;

public boolean equals(Object O)
{
if((O instance of Dog) && (((Dog)O).name == name)){
return true;
else
return false;
}
}
public int hashCode()
{return name.length();
}


Class MapTest{

public static void main(String [] args)
{

Map<Object, Object> m = new HashMap<Object, Object>();

Dog d1 = new Dog("Clover");
m.put(d1, "Dog Key");

d1.name ="magnolia"
System.out.println(m.get(d1));////prints null ouput

d1.name ="clover"
System.out.println(new Dog("clover");// prints Dog Key as output

d1.name ="arthur"
System.out.println(new Dog(clover"));// prints nulll

}
}

OUTPUT given is

null
Dog Key
null

My doubt is why is first one printing null? when we changed the key object d1's name to magnolia, it should get reflected in the Map too right?, As I understand it is the reference address of the object that gets stored in the collection(not the object itself). So when hashcode of magnolia is checked it should give 8. and the key object has name as magnolia and its hashcode should match. ??? Am I missing something???

Or, is it that the bucket number where the Object is stored does not change, based on any changes in the key object, once it is stored???


SCJP 5.0<br />SCJD working on it
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

Or, is it that the bucket number where the Object is stored does not change, based on any changes in the key object, once it is stored???

Yes, the Bucket's hashCode doesnt change inside the Map once its stored.
Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109
Thanks very much Ahmed
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Mary,

I have a question about the output mentioned.


Why does the first println display Dog Key. We are actually displaying a new object of Dog class with name "Clover". Same case with the second println statement. Just in case we have overriden toString() method, displaying the name, we would get "Clover" and "Clover" as the output. How did you get the output as Dog Key and null. Can you cross check that again?
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
We have talked a bit about that in this Post.

hth
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Yeah, I understood that. But I was worrying if Mary had forgotten to include m.get(new Dog("Clover")); rather than just new Dog("Clover"); I guess it might be a typo error.
Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109
Hi Chandra,

Thanks for pointing out that. that was a typo error....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get elements from HashMap
 
Similar Threads
Generics (Using Maps) KB exam book page 583- 586
Using object as keys in maps
Doubt in Collection
K & B - Generics & Collections