Results 1 to 5 of 5
  1. #1
    java_on_fire is offline Member
    Join Date
    Mar 2010
    Posts
    19
    Rep Power
    0

    Post Hashmap – trying to understand hashCode(), equals(), get()

    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")));  
              
            }  
      
    }
    1.I understood that the hashCode() gets called every time you call put and get method of the HashMap, correct?
    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!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Hashmap – trying to understand hashCode(), equals(), get()

    Inside the code for HashMap.get() it does:
    Java Code:
                if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
    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).

    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().

  3. #3
    java_on_fire is offline Member
    Join Date
    Mar 2010
    Posts
    19
    Rep Power
    0

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

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

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

  5. #5
    java_on_fire is offline Member
    Join Date
    Mar 2010
    Posts
    19
    Rep Power
    0

    Default Re: Hashmap – trying to understand hashCode(), equals(), get()

    Tolls, i opened the HashMap source file and found the juice inside the get() method. Thank you, you opened my eyes on something that i have never thought of doing! is looking at code inside the HashMap.

Similar Threads

  1. equals() and HashCode()
    By jomypgeorge in forum New To Java
    Replies: 1
    Last Post: 02-10-2011, 07:34 AM
  2. hashCode() contract
    By jomypgeorge in forum New To Java
    Replies: 3
    Last Post: 01-14-2011, 11:32 AM
  3. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  4. Why Equals method should be over ridden in Hashcode?
    By skyineyes in forum New To Java
    Replies: 1
    Last Post: 05-26-2008, 04:13 PM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •