Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeMap

 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Suresh Thota
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Serghei,
TreeMap is sorted on keys, not values.
Cheers
-Suresh
 
Ashok C. Mohan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic