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.
Syntax:
public Comparator comparator()
Return Value: 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
Below are the examples to illustrate the descendingIterator() method
Example 1: For Natural ordering
// Java program to demonstrate // comparator() method for natural ordering import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of TreeMap NavigableMap<Integer, String> treemap = new TreeMap<Integer, String>(); // populating tree map treemap.put(1, "one"); treemap.put(2, "two"); treemap.put(3, "three"); treemap.put(4, "four"); treemap.put(5, "five"); // pritnig the TreeMap System.out.println("TreeMap: " + treemap); // getting used Comparator in the map // using comparator() method Comparator comp = treemap.comparator(); // pritnig the comparator value System.out.println("Comparator value: " + comp); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
TreeMap: {1=one, 2=two, 3=three, 4=four, 5=five}
Comparator value: null
Example 2: For Reverse ordering
// Java program to demonstrate // comparator() method // for reverse ordering import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of TreeMap NavigableMap<Integer, String> treemap = new TreeMap<Integer, String>( Collections.reverseOrder()); // populating tree map treemap.put(1, "one"); treemap.put(2, "two"); treemap.put(3, "three"); treemap.put(4, "four"); treemap.put(5, "five"); // pritnig the TreeMap System.out.println("TreeMap: " + treemap); // getting used Comparator in the map // using comparator() method Comparator comp = treemap.comparator(); // pritnig the comparator value System.out.println("Comparator value: " + comp); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
TreeMap: {5=five, 4=four, 3=three, 2=two, 1=one}
Comparator value: java.util.Collections$ReverseComparator@232204a1
Output:
Comparator value: java.util.Collections$ReverseComparator@232204a1
Recommended Posts:
- Java | Collectors maxBy(Comparator comparator) with Examples
- Java | Collectors minBy(Comparator comparator) with Examples
- SortedMap comparator() method in Java with Examples
- Comparator nullsLast() method in Java with examples
- Comparator comparingDouble() method in Java with examples
- Comparator thenComparingLong() method in Java with examples
- ConcurrentSkipListSet comparator() method in Java with Examples
- Comparator thenComparingDouble() method in Java with examples
- Comparator nullsFirst() method in Java with examples
- Comparator thenComparingInt() method in Java with examples
- Comparator reverseOrder() method in Java with examples
- Comparator reversed() method in Java with examples
- Comparator comparingLong() method in Java with examples
- Comparator naturalOrder() method in Java with examples
- Stream sorted (Comparator comparator) method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



