Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Map Problem

 
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 12914
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 73
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still could not understand How HashMap and TreeMap work when removed() is invoke.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check whether This may help or not.
 
Ashish Agre
Ranch Hand
Posts: 73
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanky Sohar wrote:check whether This may help or not.

 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 211
Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic