Please check the portion in bold. I am getting "Dog Key" for the first S.O.P and null for the second one. why is this happening?
public class MapTest {
public static void main(
String[] arg){
HashMap m = new HashMap();
m.put("k1", new Dogs("aiko"));
m.put("k2", Pets.DOG);
m.put(Pets.CAT, " Cat");
Dogs d = new Dogs("clover");
m.put(d, "Dog Key");
m.put(new Cat(), " Cat key");
d.name = "arthur"
System.out.println(m.get(d));
d.name = "arthur";
System.out.println(m.get(new Dogs("Clover"))); }
}
class Dogs {
public Dogs(String n) { name = n; }
public String name ;
public boolean equals(Object o){
if ((o instanceof Dogs) && (((Dogs)o).name == name)){
return true;
}else{
return false;
}
}
public int hashCode(){
return name.length();
}
}
class Cat{}
enum Pets {DOG,CAT,HORSE}