ConcurrentSkipListSet comparator() method in Java with Examples


The comparator() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns the comparator used for ordering the elements in the given set. If natural ordering is used, null is returned.

Syntax:

public Comparator<E> comparator()

Here E is the type parameter i.e. the type of elements maintained by the set.

Return Value: The function returns the comparator that is used for ordering the elements in the given set, it returns null if natural ordering is used.

The program given below illustrates the ConcurrentSkipListSet.comparator() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate comparator()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.*;
  
class ConcurrentSkipListSetcomparatorExample1 {
    public static void main(String[] args)
    {
        // Creating first set object
        ConcurrentSkipListSet<Integer> set1
            = new ConcurrentSkipListSet<Integer>();
  
        // Creating second set object
        ConcurrentSkipListSet<Integer> set2
            = new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to first set
        set1.add(30);
        set1.add(5);
        set1.add(50);
        set1.add(20);
  
        // Ordering the elements in descending order
        set2 = (ConcurrentSkipListSet<Integer>)
                   set1.descendingSet();
  
        // Displaying the contents of the set1
        System.out.println("Contents of the set1: "
                           + set1);
  
        // Displaying the contents of the set2
        System.out.println("Contents of the set2: "
                           + set2);
  
        // Retrieving the comparator
        // used for ordering of elements
        System.out.println("The comparator"
                           + " used in the set:\n"
                           + set2.comparator());
    }
}

chevron_right


Output:

Contents of the set1: [5, 20, 30, 50]
Contents of the set2: [50, 30, 20, 5]
The comparator used in the set:
java.util.Collections$ReverseComparator@74a14482

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate comparator()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.*;
  
class ConcurrentSkipListSetcomparatorExample2 {
    public static void main(String[] args)
    {
        // Creating first set object
        ConcurrentSkipListSet<Integer> set1
            = new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to first set
        set1.add(30);
        set1.add(5);
        set1.add(50);
        set1.add(20);
  
        // Displaying the contents of the set1
        System.out.println("Contents of the set1: "
                           + set1);
  
        // Retrieving the comparator
        // used for ordering of elements
        System.out.println("The comparator used in the set: "
                           + set1.comparator());
    }
}

chevron_right


Output:

Contents of the set1: [5, 20, 30, 50]
The comparator used in the set: null

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#comparator–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.