Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading objects from a TreeMap

 
Shanna Ripley
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have stored several objects of type CD (represents details of CD to be stored in a catalogue) into a TreeMap and am trying to retrieve the contents but having trouble.
Example of the code I'm using to populate the TreeMap....


I've tried the following code to print out the details...


This is the output I get...

Michael Jackson: [CD@164f1d0d, CD@23fc4bec, CD@8dc8569]

I guess it's having problems printing out the values of the CD object. Can anyone point me in the right direction of how to do this...I want the details of the CD object also printed out.

Thanks in advance.
 
Paul Clapham
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then all you have to do is to modify the CD class so that it has a method whose signature is

and put some code in that method which returns whatever you want to see instead of the default value for toString() -- which is what you're seeing there.

(By the way this has nothing to do with the fact that you put references to CD objects into a TreeMap.)
 
Shanna Ripley
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul for your reply.

I'm still new to this so having a few problems. So I have the toString() method you suggested in the CD class, how do I call that from the code I showed previously. I'm assuming it needs to be called on a CD object.

Thanks for your help
 
vibhor sharma
Greenhorn
Posts: 19
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public String toString() , method is available at java.lang.Object class , it returns the String representation of the Object.
normally when you write System.out.Print(SomeObjectRefernce), toSting() method of Object class gets called automatically.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The full details are in the documentation. If you look up System.out, you find this link and it tells you to look for another method!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic