aspose file tools*
The moose likes Beginning Java and the fly likes TreeMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TreeMap" Watch "TreeMap" New topic
Author

TreeMap

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I am using a TreeMap i.e. dbase = TreeMap.

I am trying to printout the keys and values.

I have no problem with the keys displaying correctly but my values are not as expected i.e. I am getting:

New Entry Added :
Keys : [NV52SSD]
Values : [VehicleRecord@18a992f]

I'm a little confused as I took this as an example straight from a book.

My code is as follows:



If anyone could put any light on this it would be appreciated. I have tried using (String) vals but got no joy??
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
When objects are printed out, they are displayed as Strings by calling their toString() method. If a class does not have a toString() method, it will use the one from its super class. The default toString in the Object class just prints out the object reference (i.e. VehicleRecord@18a992f).

You can add a toString() method to your VehicleRecord class like so:

...or something similar, depending on what fields VehicleRecord has.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Stuart Gray,

Many thanks for your response. Much appreciated.

Whilst I am aware of the toString( ) method I feel that as I am using a TreeMap I have already put the values in using put:



As I have then said a Collection these should just print out - as in the example in my book/notes.

When reading further though I note that sometimes the following is used:

System.out.print("Values : ");
System.out.println( (String)vals );

but I get the following error message when compiling???

C:\java\PoliceDatabase>javac TestApp.java
.\VehicleDB.java:110: inconvertible types
found : java.util.Collection
required: java.lang.String
System.out.println( (String)vals );

I'm confused as to why I am getting this when the examples in the book have no problem?? Have I missed something here?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Your code doesn't compile because you are trying to cast a Collection to a String, which isn't going to work. Take out the cast, and override toString() in the VehicalRecord class (as Stuart Gray suggests). That will sort out your compilation problem.
[ April 25, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Many thanks for your response.

I did as you suggested and it printed out fine!

New Entry Added :
Keys : [NV52SSD]
Values : [NV52SSD 1084 FordMondeo Black]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I am a bit concerned/confused with the semantics of your store(VehicleRecord) method. First, the VR passed in is completely ignored. Second, the method creates a new VR using methods that access individual VR attributes on the VehicleDB itself. Why does VehicleDB have getReg(), getMake(), getModel(), etc?

What I would have expected to see is something like this:Of course, I don't have the rest of the code so I'm missing quite a bit of context that would help explain why it is the way it is. I'm just giving you my view as I would have coded it based on my past experience. It could wildly deviate from the requirements you were given.

[ Trimmed unchanged code ]
[ April 25, 2005: Message edited by: David Harkness ]
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
David

Many thanks for your response.

I have coded it just the way you stated above; in the end.

I was attempting to do a number of things. One of them was inheritance. I was seeing if it was feasible to use the Map and Set in an extended class as I had not seen any examples where this was implemented (it was just as good within an extended class as it is without one). We had just covered that subject so I was having a few questions going through my mind.

Thanks again for noticing it and pointing it out. Much appreciated.




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeMap