This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TreeMap" Watch "TreeMap" New topic


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.
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.

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' 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!!!
I agree. Here's the link:
subject: TreeMap
It's not a secret anymore!