TreeSet comparator() Method in Java with Examples

01 Nov, 2021
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 correctly implement the Set interface.
The comparator() method been present inside java.util.TreeSet shares an important function of setting and returning the comparator that can be used to order the elements in a TreeSet. The method returns a Null value if the set follows the natural ordering pattern of the elements.
Syntax: 
comp_set = (TreeSet)tree_set.comparator()
Parameters: The method does not take any parameters.
Return Value: The comparator set is used to order the elements of the set in a specific order. It returns a Null value if the set follows the default or natural ordering pattern. 
Here we will be proposing two examples below one earlier we will be using the natural ordering of the elements later using a specific comparator to understand it better.
Example 1: Using the natural ordering of the elements
Java
// Java Program to illustrate the use of comparator() method// While using the natural ordering of the elements  // Importing utility classesimport java.util.*;  // Main class// TreeSet Demo classpublic class GFG {      // Main driver method    public static void main(String[] args)    {          // Creating an empty TreeSet of integer type        TreeSet<Integer> tree_set = new TreeSet<Integer>();          // Adding elements to the set        // using add() method        tree_set.add(20);        tree_set.add(24);        tree_set.add(30);        tree_set.add(35);        tree_set.add(45);        tree_set.add(50);          // Printing elements inside TreeSet object        System.out.println("Tree Set values are: "                           + tree_set);          // Creating a comparator        Comparator comp = tree_set.comparator();          // Print and display the comparator values        System.out.println("Since the Comparator value is: "                           + comp);                // Display message only         System.out.println("it follows natural ordering");    }}
Tree Set values are: [20, 24, 30, 35, 45, 50]
Since the Comparator value is: null
it follows natural ordering
 
Example 2: Using a specific comparator
Java
// Java code to illustrate the use of comparator()// While using a specific comparator  // Importing Comparator and TreeSet classes// from java.util packageimport java.util.Comparator;import java.util.TreeSet;  // Class 1// Helper classclass Helper implements Comparator<String> {      // Method    // To compare two strings    public int compare(String str1, String str2)    {          String first_Str;        String second_Str;          first_Str = str1;        second_Str = str2;          // using compareTo() to ensure        return second_Str.compareTo(first_Str);    }}  // Main class// TreeSetDemo classpublic class GFG {      // Main driver method    public static void main(String[] args)    {          // Creating an empty TreeSet of string type        TreeSet<String> tree_set = new TreeSet<String>();          // Adding elements to our TreeSet object        // using add() method        tree_set.add("G");        tree_set.add("E");        tree_set.add("E");        tree_set.add("K");        tree_set.add("S");        tree_set.add("4");          // Printing elements in set before using comparator        System.out.println("Set before using the comparator: " + tree_set);          // Again creating an empty TreeSet of string type        // with reference to Helper class        TreeSet<String> tree_set1 = new TreeSet<String>(new Helper());          // Adding elements to our TreeSet object        // using add() method        tree_set1.add("G");        tree_set1.add("E");        tree_set1.add("E");        tree_set1.add("K");        tree_set1.add("S");        tree_set1.add("4");          // Printing elements in set before using comparator        System.out.println("The elements sorted in descending order:" + tree_set1);    }}
Set before using the comparator: [4, E, G, K, S]
The elements sorted in descending order:[S, K, G, E, 4]
sooda367
ayush123ngp
anikakapoor
solankimayank
Java - util package
Java-Collections
Java-Functions
java-treeset
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.