• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on hashCode

 
suraj padma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(m.get(new Dogs("Clover")));

In this statement, you are generating a new instance of Dogs and then trying to retrieve a Map entry that uses it as a key.

If the instance is brand new, how could it possibly have been previously used to record a Map entry for you to retrieve?
 
suraj padma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the quick response Bear Bibeault . I understand the point you said. But the following code gives me the output "Dog Key" . Here I'm generating a new instance of Dogs and not getting a null result.


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(new Dogs("Clover"), "Dog Key");
System.out.println(m.get(new Dogs("Clover")));

}
 
Henry Wong
author
Marshal
Pie
Posts: 21493
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following the code:

Dogs d = new Dogs("clover");
m.put(d, "Dog Key");

Okay. You just placed a Dogs object into your hashmap.

m.put(new Cat(), " Cat key");
d.name = "arthur"

At this point, you just messed up your hashmap. The keys of the hashmap aren't allowed to change. By changing them, you could have corrupted the map.

System.out.println(m.get(d));

This worked by luck. The key was changed in a such a way that the hashcode value didn't change. Furthermore, the value "arthur" wasn't previously in the map so it seemed to have not corrupted the map.

d.name = "arthur";
System.out.println(m.get(new Dogs("Clover")));

This worked as advertised. There isn't a Dogs key with the value "Clover" in the map -- it has been changed to "arthur" remember?

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21493
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by suraj padma:
Thank you for the quick response Bear Bibeault . I understand the point you said. But the following code gives me the output "Dog Key" . Here I'm generating a new instance of Dogs and not getting a null result.


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(new Dogs("Clover"), "Dog Key");
System.out.println(m.get(new Dogs("Clover")));

}


In this second case, no potential corruption of the hashmap is occuring -- not that it matters, as you seem to have gotten away with it in the first example.

You are indeed using two different Dogs objects. One to be place into the map, and the other used to find the first one. If you look at the hashcode method, they both hash to the same bucket. Furthermore, their equal methods check the reference of the string to see if they are the same -- and since they are both string constants from the pool, they are the same.

Henry
 
suraj padma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes got it. Thank you so much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic