Skip to content
March 21, 2008 / edivad

[Java] Reverse ordering a TreeMap

TreeMap is a Map that keep the element in the natural order of the key. The order can be changed providing to the contructor of the TreeMap a Comparable. So you can implement your custom one for more specific tasks. However if you only need a reverse order, you can easily use the beautiful Collections class with his reverseOrder().

Here is a quick example

Map reverseOrderedMap = new TreeMap(Collections.reverseOrder());

Here it is. Easy! :)

7 Comments

Leave a Comment
  1. Ashwin / Oct 22 2008 9:29 am

    hi… this forum, helped me solving an issue..

  2. suren / Jul 23 2010 10:44 am

    it is easy man. thanks a lot :)

  3. Manish / Dec 9 2010 1:13 pm

    Thanks Buddy!!
    It helped.

    • Manzorr / Dec 21 2010 1:44 am

      Thanks Man!!! Very helpful …

  4. Mukul / Apr 21 2011 12:17 pm

    Very helpful… Thanks :)

  5. Dave / May 23 2011 5:17 am

    Just what I was looking for. Much obliged.

  6. edivad / May 23 2011 11:22 am

    Happy to be helpful :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.