TreeMap comparator() method in Java with Examples Last Updated : 22 Nov, 2021 Comments Improve Suggest changes Like Article Like Report 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 classes import java.util.*; // Main class public 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 classes import java.util.*; // Main class public 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 Comment More infoAdvertise with us Next Article TreeMap comparator() method in Java with Examples rohitprasad3 Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions java-TreeMap +1 More Practice Tags : JavaJava-Collections Similar Reads TreeSet comparator() Method in Java with Examples TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to co 3 min read SortedMap comparator() method in Java with Examples The comparator() method of java.util.SortedMap interface 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.Syntax: public Comparator comparator() Return Value: This method returns the comparator used to order the keys in th 2 min read Comparator reversed() method in Java with examples The reversed() method of Comparator Interface in Java returns a comparator that imposes the reverse ordering of this comparator. If you use sort method of the array and passes this comparator after applying the reversed method then it will sort the array in reverse order. Syntax: default Comparator 2 min read Comparator naturalOrder() method in Java with examples The naturalOrder() method of Comparator Interface in Java returns a comparator that use to compare Comparable objects in natural order. The returned comparator by this method is serializable and throws NullPointerException when comparing null. Syntax: static <T extends Comparable<T>> Com 1 min read Comparator reverseOrder() method in Java with examples The reverseOrder() method of Comparator Interface in Java returns a comparator that use to compare Comparable objects in reverse of natural order. The returned comparator by this method is serializable and throws NullPointerException when comparing null. Syntax: static <T extends Comparable<T 1 min read Short compareTo() method in Java with Examples The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically. Syntax: public int compareTo(Short otherShort) Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared. Return type : It return 2 min read Comparator thenComparingInt() method in Java with examples The thenComparingInt(java.util.function.ToIntFunction) method of Comparator Interface in Java returns a lexicographic-order comparator with a function that extracts a int sort key. Syntax: default Comparator <T> thenComparingInt(ToIntFunction <T> keyExtractor) Parameters: This method acc 2 min read MonthDay compareTo() method in Java with Examples The compareTo() method of MonthDay class in Java compares this month-day to another month-day. Syntax: public int compareTo(MonthDay other) Parameter: This method accepts a parameter other which specifies the other month-day to compare to and not null. Returns: The function returns the comparator va 2 min read Comparator nullsLast() method in Java with examples The nullsLast (java.util.Comparator) method returns comparator that is a null-friendly comparator and considers null values greater than non-null. The null first operates by the following logic: The null element is considered to be greater than non-null. When both elements are null, then they are co 3 min read Comparator nullsFirst() method in Java with examples The nullsFirst(java.util.Comparator) method returns comparator that is a null-friendly comparator and considers null values to be less than non-null. The null first operates by the following logic: The null element is considered to be less than non-null. When both elements are null, then they are co 3 min read Like