aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on hashCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on hashCode " Watch "doubt on hashCode " New topic
Author

doubt on hashCode

suraj padma
Greenhorn

Joined: Feb 27, 2007
Posts: 5
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

Joined: Jan 10, 2002
Posts: 61661
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
suraj padma
Greenhorn

Joined: Feb 27, 2007
Posts: 5
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
Sheriff

Joined: Sep 28, 2004
Posts: 19003
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19003
    
  40

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

Joined: Feb 27, 2007
Posts: 5
yes got it. Thank you so much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt on hashCode