Results 1 to 4 of 4
  1. #1
    jaffar is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Post Understanding equals() with hascode()

    I am pasting you two code snippets.



    1. Implementing hashcode() and equals() with HashSet:

    Java Code:
    package others;
    
    import java.util.HashSet;
    import java.util.Iterator;
    
    public class HashCodeTest_HashSet
    {
    private String name;
    
    public HashCodeTest_HashSet(String name)
    {
    this.name = name;
    
    }
    
    public boolean equals ( Object o )
    {
    if ( !(o instanceof HashCodeTest_HashSet) ) {
    return false;
    }
    
    if ( name == null ) {
    return false;
    }
    
    HashCodeTest_HashSet obj = (HashCodeTest_HashSet) o;
    return name.equals(obj.name);
    }
    
    public int hashCode()
    {
    return name.hashCode();
    }
    
    public String toString()
    {
    return name;
    }
    
    public static void main(String args[])
    {
    
    HashCodeTest_HashSet obj1 = new HashCodeTest_HashSet("ABC");
    HashCodeTest_HashSet obj2 = new HashCodeTest_HashSet("PQR");
    HashCodeTest_HashSet obj3 = new HashCodeTest_HashSet("ABC");
    
    HashSet<HashCodeTest_HashSet> hs = new HashSet<HashCodeTest_HashSet>();
    
    hs.add(obj1);
    System.out.println(obj1.hashCode());
    
    hs.add(obj2);
    System.out.println(obj2.hashCode());
    
    hs.add(obj3);
    System.out.println(obj3.hashCode());
    
    Iterator<HashCodeTest_HashSet> itr = hs.iterator();
    
    while(itr.hasNext())
    System.out.println("Elements are : "+itr.next());
    
    }
    }
    2. Implementing hashcode() and equals() with HashMap:

    Java Code:
    package others;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map.Entry;
    import java.util.Set;
    
    
    
    public class HashCodeTest_HashMap
    {
    
    private String name;
    
    public HashCodeTest_HashMap(String name)
    {
    
    this.name = name;
    
    }
    
    public boolean equals ( Object o )
    {
    if ( !(o instanceof HashCodeTest_HashMap) ) {
    return false;
    }
    
    if ( name == null ) {
    return false;
    }
    
    HashCodeTest_HashMap obj = (HashCodeTest_HashMap) o;
    return name.equals(obj.name);
    }
    
    public int hashCode()
    {
    return name.hashCode();
    }
    
    public String toString()
    {
    return name;
    }
    
    public static void main(String args[])
    {
    
    HashCodeTest_HashMap obj1 = new HashCodeTest_HashMap("ABC");
    HashCodeTest_HashMap obj2 = new HashCodeTest_HashMap("PQR");
    HashCodeTest_HashMap obj3 = new HashCodeTest_HashMap("XYZ");
    
    HashMap<Integer, HashCodeTest_HashMap> hm = new HashMap<Integer, HashCodeTest_HashMap>();
    
    hm.put(1,obj1);
    System.out.println(obj1.hashCode());
    
    hm.put(2,obj2);
    System.out.println(obj2.hashCode());
    
    hm.put(1,obj3);
    System.out.println(obj3.hashCode());
    
    Set<Entry<Integer, HashCodeTest_HashMap>> set = hm.entrySet(); //doubt
    
    Iterator<Entry<Integer, HashCodeTest_HashMap>> itr = set.iterator();//doubt
    
    while(itr.hasNext())
    System.out.println("Elements are : "+itr.next());
    
    System.out.println("For Key 2 Value is : "+hm.get(2));
    }
    }

    My Question is :

    While debugging with HashSet line by line I am able to find the flow entering into equals method when its get matched with two equal hashcodes.

    But while I am debugging with HashMap line by line I am not able to find the flow entering into equals method when its get matched with two equal hashcodes.


    So I am unable to understand how and when exactly equals gets called and why its not getting called with HashMap please somebody explain me this?

    Or any link or reference to understand this...

    Thanks in advance...
    Last edited by jaffar; 06-01-2012 at 12:50 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Understanding equals() with hascode()

    A Set does not want two equal objects, a Map doesn't care if there are two values that are equal.

    Your test should look at the key for the Map, not the value.
    Last edited by Norm; 06-01-2012 at 02:09 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jaffar is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: Understanding equals() with hascode()

    Norm Thank you for your response...

    If I am not wrong I want to clarify this...

    You mean to say that since HashMap allows duplicates in it so there is no need to call equal method?

    Since Set does not allow duplicate so it calls equals() method right?

    And one more doubt :

    In map in the code after once key with 1 is added it found again key with 1 so it ignores it right ?...
    Because when i printing after adding i could find only once 1 (key) is printing along with value XYZ.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Understanding equals() with hascode()

    What does "ignores it" mean? Did you read the API doc for the methods you are using to see what they do?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. using .equals
    By droidus in forum New To Java
    Replies: 11
    Last Post: 01-21-2012, 03:33 AM
  2. c always equals a*b
    By imorio in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 02:32 PM
  3. == and equals()
    By arefeh in forum New To Java
    Replies: 13
    Last Post: 01-05-2010, 04:56 PM
  4. == is same as .equals()??
    By DrMath in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 04:57 AM
  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
  •