Wednesday, July 3, 2024
HomeLanguagesJavaSortedMap comparator() method in Java with Examples

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 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

 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments