• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt on hashCode

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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}
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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")));

}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes got it. Thank you so much
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic