• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Get elements from HashMap

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much Ahmed
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have talked a bit about that in this Post.

hth
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

Thanks for pointing out that. that was a typo error....
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic