• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Doubt in Generic Map  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is Asmita, I have a doubt in the following map code

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

class Cat {
}

enum Pets {DOG, CAT, HORSE}

class MapTest
{
public static void main(String[] args)
{
Map<Object, Object> m = new HashMap<Object, Object>();

m.put("k1", new Dog("aiko"));
m.put("k2", Pets.DOG);
m.put(Pets.CAT, "CAT key");
Dog d1 = new Dog("clover");
m.put(d1, "Dog key");
m.put(new Cat(), "Cat key");


System.out.println(m.get("k1"));
String k2 = "k2";
System.out.println(m.get(k2));
Pets p = Pets.CAT;
System.out.println(m.get(p));
System.out.println(m.get(d1));
System.out.println(m.get(new Cat()));
System.out.println(m.size());

d1.name = "magnolia"; //lines of doubt
System.out.println(m.get(d1));
d1.name = "clover";
System.out.println(m.get(new Dog("clover")));
d1.name = "arthur";
System.out.println(m.get(new Dog("clover")));
}
}

I got this code from SCJP java 5 written by Kathy Sierra.
My problem starts on the line commented as lines of doubt.
In the book it is written that after d1.name ="magnolia"; is done the invocation of m.get(d1); should give null as the hashcode method fails, but when I run this code it gives the d1 keys value. In the next invocation m.get(new Dog("clover")); it should give me the matching key value i.e Dog Key, but it gives null.

Aren't the equal & hashcode methods working . What do this methods actually do.
Please help me, Thank you for help in advance.

Asmita
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aren't the equal & hashcode methods working . What do this methods actually do.



Its the equals() and hashCode() methods. Java is case sensitive.

Henry
 
Asmita Khamkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know sir its equals() & hashcode() methods I have written correctly in code, the code is compiling and running properly but it is not giving the output as written in book.

Asmita
 
Henry Wong
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Asmita Khamkar wrote:I know sir its equals() & hashcode() methods I have written correctly in code



hashCode()... with the capital C.

Asmita Khamkar wrote:the code is compiling and running properly but it is not giving the output as written in book.



What you are describing, particularly here...

In the next invocation m.get(new Dog("clover")); it should give me the matching key value i.e Dog Key, but it gives null.



Is exactly what should happen if you misspell the hashCode() method.

Henry
 
Asmita Khamkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help sir got my mistake Thank you very much.

Asmita
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!