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")));
}
}