Java TreeSet.comparator()

Syntax

TreeSet.comparator() has the following syntax.

public Comparator <? super E> comparator()

Example

In the following code shows how to use TreeSet.comparator() method.


import java.util.Collections;
import java.util.TreeSet;
/*from w ww  .j  ava  2 s  .c  o m*/
public class Main {
   public static void main(String[] args) {
      TreeSet <Integer> tree = new TreeSet<Integer>(Collections.reverseOrder());
          
      tree.add(12);
      tree.add(13);
      tree.add(14);
      tree.add(15);
      tree.add(16);
      tree.add(17);
     
      // using comparator
      System.out.println(tree.comparator());
      
   
   }    
}

The code above generates the following result.