TreeSet comparator() Method in Java with Examples Last Updated : 01 Nov, 2021 Comments Improve Suggest changes Like Article Like Report 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 classes import java.util.*; // Main class // TreeSet Demo class public 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"); } } Output: 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 package import java.util.Comparator; import java.util.TreeSet; // Class 1 // Helper class class 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 class public 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); } } OutputSet before using the comparator: [4, E, G, K, S] The elements sorted in descending order:[S, K, G, E, 4] Comment More infoAdvertise with us Next Article SortedSet contains() method in Java with Examples chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-treeset +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads SortedSet Interface in Java with Examples The SortedSet interface is present in java.util package extends the Set interface present in the collection framework. It is an interface that implements the mathematical set. This interface contains the methods inherited from the Set interface and adds a feature that stores all the elements in this 8 min read SortedSet add() method in Java with Examples The add() method of SortedSet in Java is used to add a specific element into a Set collection. The function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set. Syntax: boolean add(E element) Wh 2 min read SortedSet addAll() method in Java with Examples The addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be ad 2 min read SortedSet clear() method in Java with Examples The clear() method is used to remove all the elements from a SortedSet. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The method doe 1 min read 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 SortedSet contains() method in Java with Examples The contains() method is used to check whether a specific element is present in the SortedSet or not. So basically it is used to check if a SortedSet contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of SortedSet. This is the e 2 min read SortedSet containsAll() method in Java with Examples The containsAll() method of Java SortedSet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set. Syntax: public boolean containsAll(Collection C) Parameters: The parameter C 2 min read SortedSet first() method in Java The first() method of SortedSet interface in Java is used toReturns the first i.e., the lowest element currently in this set.Syntax: E first() Where, E is the type of element maintained by this Set.Parameters: This function does not accepts any parameter.Return Value: It returns the first or the low 2 min read SortedSet hashCode() method in Java with Examples The hashCode() method of SortedSet in Java is used to get the hashCode value for this instance of the SortedSet. It returns an integer value which is the hashCode value for this instance of the SortedSet. Syntax: public int hashCode() Parameters: This function has no parameters. Returns: The method 2 min read SortedSet headSet() method in Java The headSet() method of SortedSet interface in Java is used to return a view of the portion of this set whose elements are strictly less than the parameter toElement. The set returned by this method is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The 2 min read Like