HashMap Doubt

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 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

posted Aug 10, 2006 05:23:00

0

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.

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.

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.

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com