Results 1 to 5 of 5
- 09-14-2011, 05:06 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 19
- Rep Power
- 0
Hashmap trying to understand hashCode(), equals(), get()
1.I understood that the hashCode() gets called every time you call put and get method of the HashMap, correct?Java Code:import java.util.*; class Dog{ public String name; public Dog(String n) { name = n; } public boolean equals(Object o){ System.out.println("i'm inside equals"); if((o instanceof Dog) && (((Dog)o).name == this.name)){ return true;} else{ return false;} } public int hashCode(){ return name.length(); } } public class MapTest { public static void main(String[] args) { Map<Object, Object> m = new HashMap<Object, Object>(); Dog d1 = new Dog("clover"); m.put(d1, "dog key"); System.out.println(m.get(d1)); d1.name = "magnolia"; System.out.println(m.get(d1)); d1.name = "arthur"; System.out.println(m.get(new Dog("clover"))); } }
2.The equals() method, where is it getting the Object o and the this reference from?
3.If I change the code System.out.println(m.get(new Dog("clover"))); at bottom after d1.name = arthur to
System.out.println("d1= " + map.get(d1));
I get:
d1= Dog key
The equals method never got executed because i put System.out.println("i'm inside equals");
but never got executed? how come? and why i'm not getting null instead of Dog key?
Please help, i have been struggling trying to find an answer but no luck!
- 09-14-2011, 05:25 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Hashmap trying to understand hashCode(), equals(), get()
Inside the code for HashMap.get() it does:
So it first checks the entry hash with your hash, then it checks if the keys are == (ie the same object) then if they are equal() (the method).Java Code:if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
In your first case they are == (and the hash matches) so you get "dog key" back.
In the second case (and this highlights why changing key values is a Bad Thing) your hash does not match the entry hash...because the entry has been filed under a hash of 6 (the length of "clover") and you are asking it to look for an entry under 8 (the length of "magnolia"). So it fails on the first check.
In the third case the hashes match ("clover" is 6 characters long), but the equals() fails ("clover" != "arthur").
When you change that call to using d1, the hashes match and the objects are == (they are the same object) so it never goes into equals().
- 09-14-2011, 05:34 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 19
- Rep Power
- 0
Re: Hashmap trying to understand hashCode(), equals(), get()
Hi Tolls, thank you for your prompt reply, please allow me time to make this sink before getting back with further questions if needed.
Thanks again, i truly appreciate it, you would believe how much time i spent trying to figure this out i even had nightmares about it!!
- 09-14-2011, 05:38 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Hashmap trying to understand hashCode(), equals(), get()
I only looked up how it did it because I thought it would only check the hash and then do equals(), so I wanted to see exactly what it was doing.
- 09-15-2011, 06:39 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
equals() and HashCode()
By jomypgeorge in forum New To JavaReplies: 1Last Post: 02-10-2011, 07:34 AM -
hashCode() contract
By jomypgeorge in forum New To JavaReplies: 3Last Post: 01-14-2011, 11:32 AM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM -
Why Equals method should be over ridden in Hashcode?
By skyineyes in forum New To JavaReplies: 1Last Post: 05-26-2008, 04:13 PM -
name clash: equals(E) in and equals(java.lang.Object)
By AdRock in forum New To JavaReplies: 0Last Post: 01-25-2008, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks