• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help  RSS feed

 
Shayan Siddiqui
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please tell me why the following code is giving me
Dog key
null
null
as output
Is there something wrong i have written in the following code...

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 Test
{
public static void main(String args[])
{
Map<Object, Object> mlist=new HashMap<Object,Object>();
Dog d1=new Dog("clover");
Dog d2=new Dog("clover");
mlist.put(d1,"Dog key");
System.out.println(mlist.get(d1));
System.out.println(mlist.get(d2));
System.out.println(mlist.get(new Dog("clover")));

}
}
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the hashCode() method -- with a capital C.

Henry
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shayan please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!