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

TreeMap

Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
Hi all. I have the next question about subject:

I thought that the output would be Bill Carl Kathy because TreeMap is sorted by natural order, but output is Bill Kathy Carl.
Why?
Approximately the same example with TreeSet print out results in alfabetical (natural) order.
Thanks in advance.
[ April 05, 2004: Message edited by: Serghei Jelauc ]

SCJP 1.4 <br />SCBCD 1.3<br />SCWCD 1.4
Suresh Thota
Ranch Hand

Joined: Sep 24, 2003
Posts: 152
Hi Serghei,
TreeMap is sorted on keys, not values.
Cheers
-Suresh


SCJP 1.6
Ashok C. Mohan
Ranch Hand

Joined: Dec 03, 2003
Posts: 75
The TreeMap class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.

The sorting in TreeMap is done on the keys and not the values.Since you have given the keys as 'a','b','c'.....it will be sorted in that order...


SCJP 1.4
Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment.
Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
Thanks guys!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: TreeMap