TreeMap comparator() method in Java with Examples

The comparator() method of java.util.TreeMap class is used to return the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
--> java.util Package
--> TreeMap Class
--> comparator() Method
Syntax:Â Â
public Comparator comparator()
Return Type: This method returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
Note: descendingIterator() method is natural ordering by default.
Example 1: For Natural orderingÂ
Â
Java
// Java program to Illustrate comparator() Method// for Natural Ordering (Descending Order)Â
// Importing required classesimport java.util.*;Â
// Main classpublic class GFG {Â
    // Main driver method    public static void main(String[] argv) throws Exception    {Â
        // Try block to check for exceptions        try {Â
            // Creating an empty TreeMap by            // creating object of NavigableMap            NavigableMap<Integer, String> treemap                = new TreeMap<Integer, String>();Â
            // Populating TreeMap            // using put() method            treemap.put(1, "one");            treemap.put(2, "two");            treemap.put(3, "three");            treemap.put(4, "four");            treemap.put(5, "five");Â
            // Printing the TreeMap            System.out.println("TreeMap: " + treemap);Â
            // Getting used Comparator in the map            // using comparator() method            Comparator comp = treemap.comparator();Â
            // Printing the comparator value            System.out.println("Comparator value: " + comp);        }Â
        // Catch block to handle the exception        catch (NullPointerException e) {Â
            // Display message if exception occurs            System.out.println("Exception thrown : " + e);        }    }} |
Output:Â
TreeMap: {1=one, 2=two, 3=three, 4=four, 5=five}
Comparator value: null
Â
Example 2: For Reverse ordering
Java
// Java program to demonstrate comparator() Method// for Reverse OrderingÂ
// Importing required classesimport java.util.*;Â
// Main classpublic class GFG {Â
    // Main driver method    public static void main(String[] argv) throws Exception    {Â
        // Try block to check for exceptions        try {Â
            // Creating an empty TreeMap            NavigableMap<Integer, String> treemap                = new TreeMap<Integer, String>(                    Collections.reverseOrder());Â
            // Populating TreeMap            // using put() method            treemap.put(1, "one");            treemap.put(2, "two");            treemap.put(3, "three");            treemap.put(4, "four");            treemap.put(5, "five");Â
            // Printing the TreeMap            System.out.println("TreeMap: " + treemap);Â
            // Getting used Comparator in the map            // using comparator() method            Comparator comp = treemap.comparator();Â
            // Printing the comparator value            System.out.println("Comparator value: " + comp);        }Â
        // Catch block to handle the exceptions        catch (NullPointerException e) {Â
            // Display message if exception occurs            System.out.println("Exception thrown : " + e);        }    }} |
Output:Â
TreeMap: {5=five, 4=four, 3=three, 2=two, 1=one}
Comparator value: java.util.Collections$ReverseComparator@232204a1
Â



