Open In App

ConcurrentSkipListSet clear() method in Java

Last Updated : 17 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.concurrent.ConcurrentSkipListSet.clear() method is an in-built function in Java which removes all the elements from this set.

Syntax:

 ConcurrentSkipListSet.clear()

Parameters: The function does not accept any parameter.

Return Value: The function does not return anything.

Below programs illustrate the ConcurrentSkipListSet.clear() method:

Program 1:




/* Java Program Demonstrate clear()
method of ConcurrentSkipListSet() */
import java.util.concurrent.*;
class ConcurrentSkipListSetClearExample1 {
    public static void main(String[] args)
    {
        // Creating a set object
        ConcurrentSkipListSet<Integer> Lset = 
                   new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        for (int i = 10; i <= 50; i += 10)
            Lset.add(i);
  
        // Printing elements of the set
        System.out.println("The set contains: " + Lset);
  
        // Clear the set
        Lset.clear();
  
        // Printing elements of the set after clear operation
        System.out.println("After clear operation the set contains: " 
                                                             + Lset);
    }
}


Output:

The set contains: [10, 20, 30, 40, 50]
After clear operation the set contains: []

Program 2:




/* Java Program Demonstrate clear()
method of ConcurrentSkipListSet() */
import java.util.concurrent.*;
class ConcurrentSkipListSetClearExample2 {
    public static void main(String[] args)
    {
        // Creating a set object
        ConcurrentSkipListSet<String> Lset = 
                    new ConcurrentSkipListSet<String>();
  
        // Adding elements to this set
        Lset.add("alex");
        Lset.add("bob");
        Lset.add("chuck");
        Lset.add("drake");
        Lset.add("eric");
  
        // Printing elements of the set
        System.out.println("The set contains: " + Lset);
  
        // Clear the set
        Lset.clear();
  
        // Printing elements of the set after clear operation
        System.out.println("After clear operation the set contains: " 
                                                              + Lset);
    }
}


Output:

The set contains: [alex, bob, chuck, drake, eric]
After clear operation the set contains: []

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads