SortedMap comparator() method in Java with Examples Last Updated : 08 Jun, 2021 Comments Improve Suggest changes Like Article Like Report 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 this map, or null if this map uses the natural ordering of its keys.Below programs illustrate the comparator() method:Example 1: For Natural ordering. Java // 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 SortedTreeMap SortedMap<Integer, String> sotreemap = new TreeMap<Integer, String>(); // Populating tree map sotreemap.put(1, "one"); sotreemap.put(2, "two"); sotreemap.put(3, "three"); sotreemap.put(4, "four"); sotreemap.put(5, "five"); // Printing the SortedTreeMap System.out.println("SortedTreeMap: " + sotreemap); // Getting used Comparator in the map // using comparator() method Comparator comp = sotreemap.comparator(); // Printing the comparator value System.out.println("Comparator value: " + comp); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } Output: SortedTreeMap: {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 import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception<div class="code-output"> <b>Output:</b> <pre> Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You} The set is: [10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You] </pre> </div> { try { // Creating object of TreeMap SortedMap<Integer, String> sotreemap = new TreeMap<Integer, String>( Collections.reverseOrder()); // Populating tree map sotreemap.put(1, "one"); sotreemap.put(2, "two"); sotreemap.put(3, "three"); sotreemap.put(4, "four"); sotreemap.put(5, "five"); // Printing the TreeMap System.out.println("SortedTreeMap: " + sotreemap); // Getting used Comparator in the map // using comparator() method Comparator comp = sotreemap.comparator(); // Printing the comparator value System.out.println("Comparator value: " + comp); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } Output: SortedTreeMap: {5=five, 4=four, 3=three, 2=two, 1=one} Comparator value: java.util.Collections$ReverseComparator@232204a1 Comment More infoAdvertise with us Next Article SortedMap values() method in Java with Examples chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-SortedMap +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads SortedMap Interface in Java SortedMap is an interface in the collection framework that is a part of java.util package and extends the Map interface. It represents a map that maintains its keys in a sorted order. The keys in a SortedMap are sorted according to their natural ordering or by a Comparator provided at the time of ma 10 min read SortedMap tailMap() method in Java The tailMap() method of SortedMap interface in Java is used to return a view of the portion of this map whose keys are greater than or equal to fromKey. The map returned by this method is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The map returned b 3 min read SortedMap firstKey() method in Java The firstKey() method of SortedMap interface in Java is used to return the first or the lowest key currently in this map. Syntax: K firstKey() Where, K is the type of key maintained by this Set. Parameters: This function does not accepts any parameter. Return Value: It returns the first print the lo 2 min read SortedMap lastKey() method in Java The lastKey() method of SortedMap interface in Java is used to return the last or the greatest key currently in this map. Syntax: K lastKey() Where, K is the type of key maintained by this Set. Parameters: This function does not accepts any parameter. Return Value: It returns the last or the greates 2 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 SortedMap values() method in Java with Examples The values() method of SortedMap interface in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the Map. Syntax: SortedMap.values() Parameters: The method does not accept any parameters. Return Value: The method is used to retur 2 min read SortedMap keySet() method in Java with Examples The keySet() method of SortedMap Interface in Java is used to create a set out of the key elements contained in the treemap. It basically returns a set view of the keys or we can create a new set and store the key elements in them in an ascending order. Since the set is backed by the map, any change 2 min read SortedMap entrySet() method in Java with Examples The entrySet() method of SortedMap interface in Java is used to create a set out of the same elements contained in the map. It basically returns a set view of the map or creates a new set and store the map elements into them. Syntax: SortedMap.entrySet() Parameters: The method does not take any para 2 min read Like