• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Reading objects from a TreeMap

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 27667
89
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 19
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 77903
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic