| Author |
Map Problem
|
Aashu Mahajan
Ranch Hand
Joined: May 27, 2011
Posts: 110
|
|
Source : Written by Me.
output :
{aa=AA, bb=BB, cc=CC, aa=AA, dd=DD, ee=EEE}
{aa=AA, bb=BB, cc=CC, aa=AA, dd=DD, ee=EEE}
I could not understand the output and also at Line-1 when i remove 'a2' then why it appear in the second line of output??
I am bit confused about Map , please could any explain.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3065
|
|
|
Hi Aashu. The keys you are using are not "consistent with equals". Read the documentation of TreeMap and Comparable carefully.
|
 |
Ashish Agre
Ranch Hand
Joined: Jan 22, 2011
Posts: 73
|
|
Just replace the line 38-39
with this code
String o = hasht.remove(a2); //Line-1
System.out.println(hasht + " " + o); //output : {..Map data...} null
you actually did not removed any thing from the map
Java Doc Returns:
the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)
|
| B.E IT | SCJP 6.0 98 % |
|
 |
Aashu Mahajan
Ranch Hand
Joined: May 27, 2011
Posts: 110
|
|
I still could not understand How HashMap and TreeMap work when removed() is invoke.
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1047
|
|
check whether This may help or not.
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
Ashish Agre
Ranch Hand
Joined: Jan 22, 2011
Posts: 73
|
|
Shanky Sohar wrote:check whether This may help or not.
|
 |
Ikpefua Jacob-Obinyan
Ranch Hand
Joined: Aug 31, 2010
Posts: 394
|
|
Stephan Van Hulst wrote:The keys you are using are not "consistent with equals"
Hi Guys, @Stephan I agree with you...@Aashu I guess your implementation of the compareTo() method is the problem, the implementation makes the maps keys NOT consistent with the equals method. May I ask why you used that implementation?. Your map did NOT even recognise your keys in the first place...If you replace line 38 of your code with it returns null! ... ... This means the Map did NOT recognise your keys.
Lets try another implementation;
Output:
P.S. I modified line 38 of your code to give us the value of the removed key.
Regards
Ikpefua
|
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
|
 |
Sagar Shroff
Ranch Hand
Joined: Jun 07, 2011
Posts: 196
|
|
In K.S book it has written that if you do not implement hashcode() and equals() then you will not be able to get the value.But when i tried this program at home and called get() i got the appropriate value.
Why soo???
Please Please Help me !!
|
OCJP-90%,OCPWCD-95%
|
 |
Ikpefua Jacob-Obinyan
Ranch Hand
Joined: Aug 31, 2010
Posts: 394
|
|
sagar shroff wrote:In K.S book it has written that if you do not implement hashcode() and equals() then you will not be able to get the value.But when i tried this program at home and called get() i got the appropriate value.
Why soo???
You are talking about page 583 of the K&B book yes...Now this is a caption of what the book says: "Any classes that you use as a part of the keys for that map MUST override (I repeat override NOT implement) the hashCode() and equals() methods"...Okay you are correct, when you look at the program it is a little bit confusing (at first sight) because the HashIt class does NOT effectively override the hashCode() and equals() methods but the actual keys used are Strings and if you check the API you will see that Strings and Wrappers effectively override the hashCode() and equals() methods.
Here is a little program to demonstrate what I am trying to say;
Output:
The output may clarify your doubts and you can clearly see that it fulfils the fundamental aspect of the hashCode()/equals() contract which says if two objects return true according to the equals() method test, their hashCodes() MUST be equal.
I hope this helps
Phew what a long day...I am going to have dinner...I will be right back.
Regards
Ikpefua.
|
 |
Sagar Shroff
Ranch Hand
Joined: Jun 07, 2011
Posts: 196
|
|
Thank You So Much !!!
And also Making it SO simple To UNDERSTAND....Now i am so cleared !!! ..Phew !!
Thanks Ikpefua Jacob-Obinyan
|
 |
 |
|
|
subject: Map Problem
|
|
|