beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to iterate TreeMap in reverse order in Java

By Chaitanya Singh | Filed Under: Java Collections

By default TreeMap elements are sorted in ascending order of keys. We can iterate the TreeMap in reverse order to display the elements in descending order of keys.

Display TreeMap elements in reverse order:

import java.util.*;

class TreeMapDemo {
  public static void main(String args[]) {
 
    Map<String, String> treemap = 
      new TreeMap<String, String>(Collections.reverseOrder());

    // Put elements to the map
    treemap.put("Key1", "Jack");
    treemap.put("Key2", "Rick");
    treemap.put("Key3", "Kate");
    treemap.put("Key4", "Tom");
    treemap.put("Key5", "Steve");
 
    Set set = treemap.entrySet();
    Iterator i = set.iterator();
    // Display elements
    while(i.hasNext()) {
      Map.Entry me = (Map.Entry)i.next();
      System.out.print(me.getKey() + ": ");
      System.out.println(me.getValue());
    }
  }
}

Output:

Key5: Steve
Key4: Tom
Key3: Kate
Key2: Rick
Key1: Jack

As you can see elements are displayed in the reverse order of keys.

More about Collections.reverseOrder() from javadoc:
public static Comparator reverseOrder(): Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface. (The natural ordering is the ordering imposed by the objects’ own compareTo method.) This enables a simple idiom for sorting (or maintaining) collections (or arrays) of objects that implement the Comparable interface in reverse-natural-order. For example, suppose a is an array of strings. Then: Arrays.sort(a, Collections.reverseOrder());

sorts the array in reverse-lexicographic (alphabetical) order. The returned comparator is serializable.

Returns:
A comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.

Reference:
Collections.reverseOrder()

Enjoyed this post? Try these related posts

  1. How to remove repeated elements from ArrayList?
  2. Queue Interface in Java Collections
  3. HashMap in Java with Example
  4. Java ArrayList ensureCapacity() Method example
  5. LinkedHashMap in Java
  6. Java ArrayList isEmpty() Method example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap