• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt on Collections from K&B book

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code is taken from K&b book Page 561

import java.util.*;
class Dog
{
public String name;
public Dog(String n)
{
name=n;
}
public boolean equals(Object o)
{
if((o instanceof Dog) && (((Dog) o).name==name))
return true;
else
return false;
}

public int hashcode()
{
return name.length();
}
}

public 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");
System.out.println(m.get(d1));
d1.name="magnolia";
System.out.println(m.get(d1));
}
}


Here the Object used as a key has its value changed. So it must give the
output as
Dog Key
null
But when i run the program i got the output as
Dog Key
Dog Key

Can Any one explain the reason?

Thanks
Sravani
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sravani,

Welcome to the JavaRanch!


put the System.out.println(d1.hashCode()); before and after changing dog's
name to see that.


[EDIT:] you have not overridden the hashCode() of the Object class. It is hashCode() not hashcode(). Now changing the name will change the hashCode()
and you will get the result you thought to be.

Thanks,
[ June 07, 2007: Message edited by: Chandra Bhatt ]
 
sravani jetty
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,
When i call d1.hashcode() before changing the name i got 6 and i got 8 after chaging the name as the hascode method is overridden in Dog which returns length of the name.

Thanks
Sravani
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See your hashCode() method. You have not overridden the hashCode(). It is
hashCode() not hashcode().

Thanks,
[ June 07, 2007: Message edited by: Chandra Bhatt ]
 
sravani jetty
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't noticed it. Thank you
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic