ConcurrentSkipListSet in Java


The ConcurrentSkipListSet has elements that are sorted by default. Also, its implementation is based on the ConcurrentSkipListMap. The ConcurrentSkipListSet class also implements the Collection interface as well as the AbstractSet class. It is a part of the Java Collection Framework.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.util.concurrent.ConcurrentSkipListSet;
public class Demo {
   public static void main(String[] args) {
      ConcurrentSkipListSet<Integer> csls = new ConcurrentSkipListSet<Integer>();
      csls.add(7);
      csls.add(4);
      csls.add(1);
      csls.add(9);
      csls.add(3);
      System.out.println("The elements in ConcurrentSkipListSet are: " + csls);
   }
}

The output of the above program is as follows −

Output

The elements in ConcurrentSkipListSet are: [1, 3, 4, 7, 9]

Now let us understand the above program.

The ConcurrentSkipListSet is created and then elements are added to it. Then the elements are displayed and these are sorted by default. A code snippet that demonstrates this is given as follows −

ConcurrentSkipListSet<Integer> csls = new ConcurrentSkipListSet<Integer>();
csls.add(7);
csls.add(4);
csls.add(1);
csls.add(9);
csls.add(3);
System.out.println("The elements in ConcurrentSkipListSet are: " + csls);

Updated on: 30-Jul-2019

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements