This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap Doubt

 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the Above code, I used an object of class Dog as a key. My question is in class Dog, I didnt ovveride the equals() and the hashcode(). I realize this should compile and run nonetheless but shouldnt the last output give me a null as a result of that??
Instead it is showing me the value associated with d1.
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

check code above you will get your answer
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gowher Thanks for your response, But The code you have given me will result in a null regardless of whether equals() and hashcode() is overrriden or not.
I had given this:
m.put(d1,"Dog Key");
in your code you have given:
out.println(m.get(new Dog("New dog")));//here is answer for you

here get() takes a key, if you give it a value(and not a key), it will always give a null(unless you flip the order but thats another story).

What i wanted to know is that if I use a Dog object as key, and I dont override the equals(0 and hashcode() why is it still showing me the value associated with that key. isnt that a violation of a Hashmap/table contract that although you can add an object of a class that doesnt ovveride equlas or hashcode, the value associated with that key will not be found.?
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use keys as string use keys as Dog objects.
Below is code comment hashCode method and equals
method and see what is output and understand what
is use of equals method and how is hashCode method
linked with equals method.
 
sanjeev mehra
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amitabha Batranab

In the Above code, I used an object of class Dog as a key. My question is in class Dog, I didnt ovveride the equals() and the hashcode(). I realize this should compile and run nonetheless but shouldnt the last output give me a null as a result of that??
Instead it is showing me the value associated with d1.



output:
hello world - String's default method is toSting(), which prints it's value.

Same way:
out.println(m.get(d1)); returns object refernced to d1 that is "Dog Key"; and this is String object having toString() as default method; toString() prints value of String object.
 
Eleanor Leong
Greenhorn
Posts: 21
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you did not overide the equals() and the hashCode(). the default
equals() and the hashCode() of Class Object is used.
Since you are using d reference which points to the Dog Object which you stored as a key in the Map. the reference d and the key you stored in the Map are pointing to the same Dog object, i.e.
Map key -----> Dog Object
d ---------------|

The hashCode() on d reference will return the address of the Dog Object which is the same as the key in Map. Since they are pointing to the same Dog Object, they are equal according to the equals() of Object.

Hope this is clear enough.
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amitabha Batranab:
I realize this should compile and run nonetheless but shouldnt the last output give me a null as a result of that??
Instead it is showing me the value associated with d1


Since the Dog class doesnot override equals() and hashcode(), the equals() and hashcode() methods from Object class will be used. The default hashcode() method typically (though not always) uses the internal address representation of the object. Since d1 is used in both places, the two usages end up having same hashcode(). further, the equals() method from Object class simply returns the result of "==". Since both the references refer to the same object on the heap, the equals() method also returns true. Hence the get() method is able to find the key in the map and hence it retrives the value associated with it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic