• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map Problem

 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aashu. The keys you are using are not "consistent with equals". Read the documentation of TreeMap and Comparable carefully.
 
Ashish Agre
Ranch Hand
Posts: 73
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still could not understand How HashMap and TreeMap work when removed() is invoke.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check whether This may help or not.
 
Ashish Agre
Ranch Hand
Posts: 73
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:check whether This may help or not.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Sagar Shroff
Ranch Hand
Posts: 209
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You So Much !!!

And also Making it SO simple To UNDERSTAND....Now i am so cleared !!! ..Phew !!

Thanks Ikpefua Jacob-Obinyan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic