Results 1 to 4 of 4
- 06-01-2012, 09:25 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
Understanding equals() with hascode()
I am pasting you two code snippets.
1. Implementing hashcode() and equals() with HashSet:
2. Implementing hashcode() and equals() with HashMap: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()); } }
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
- 06-01-2012, 02:01 PM #2
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.
- 06-01-2012, 02:18 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
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.
- 06-01-2012, 02:32 PM #4
Similar Threads
-
using .equals
By droidus in forum New To JavaReplies: 11Last Post: 01-21-2012, 03:33 AM -
c always equals a*b
By imorio in forum New To JavaReplies: 3Last Post: 11-12-2010, 02:32 PM -
== and equals()
By arefeh in forum New To JavaReplies: 13Last Post: 01-05-2010, 04:56 PM -
== is same as .equals()??
By DrMath in forum New To JavaReplies: 1Last Post: 09-30-2009, 04:57 AM -
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