The java.util.TreeSet.comparator() method shares an important function of setting and returning the comparator that can be used to order the elements in a TreeSet. The method returns 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 method returns the comparator set 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.
Below are the programs illustrating the use of java.util.TreeSet.comparator() method:
Program 1: When using natural ordering of the elements:
// Java code to illustrate comparator() import java.util.*; public class Tree_Set_Demo { public static void main(String[] args) { // Creating an empty TreeSet TreeSet<Integer> tree_set = new TreeSet<Integer>(); // Adding elemetns to the set tree_set.add(20); tree_set.add(24); tree_set.add(30); tree_set.add(35); tree_set.add(45); tree_set.add(50); System.out.println("Tree Set values are: "+ tree_set); // Creating a comparator Comparator comp = tree_set.comparator(); // Displaying the comparator values System.out.println("Since the Comparator value is: "+ comp); 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
Program 2: When using a specific comparator.
// Java code to illustrate the use of comparator() import java.util.Comparator; import java.util.TreeSet; class The_Comparator implements Comparator<String> { public int compare(String str1, String str2) { String first_Str; String second_Str; first_Str = str1; second_Str = str2; return second_Str.compareTo(first_Str); } } public class Tree_Set_Demo { public static void main(String[] args) { TreeSet<String> tree_set = new TreeSet<String>(new The_Comparator()); tree_set.add("G"); tree_set.add("E"); tree_set.add("E"); tree_set.add("K"); tree_set.add("S"); tree_set.add("4"); System.out.println("Set before using the comparator: "+ tree_set); System.out.println("The elements sorted in descending"+ "order:"); for (String element : tree_set) System.out.print(element + " "); } } |
Set before using the comparator: [S, K, G, E, 4] The elements sorted in descendingorder: S K G E 4
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Stream sorted (Comparator comparator) method in Java
- Java | Collectors maxBy(Comparator comparator) with Examples
- Java | Collectors minBy(Comparator comparator) with Examples
- PriorityQueue comparator() Method in Java
- PriorityBlockingQueue comparator() method in Java
- TreeMap comparator() method in Java with Examples
- SortedMap comparator() method in Java with Examples
- ConcurrentSkipListSet comparator() method in Java with Examples
- Comparator nullsFirst() method in Java with examples
- Comparator nullsLast() 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
- Comparator thenComparingDouble() method in Java with examples
- Comparator comparingDouble() method in Java with examples
- Comparator thenComparingLong() method in Java with examples
- Comparator Interface in Java with Examples
- Comparable vs Comparator 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.

