aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashMap Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashMap Doubt" Watch "HashMap Doubt" New topic
Author

HashMap Doubt

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361


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

Joined: Feb 07, 2005
Posts: 643

check code above you will get your answer
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
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

Joined: Feb 07, 2005
Posts: 643
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

Joined: Feb 12, 2001
Posts: 93
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

Joined: Mar 24, 2006
Posts: 21
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

Joined: Jul 20, 2006
Posts: 107
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap Doubt