ConcurrentSkipListSet removeAll() method in Java
The removeAll() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns removes from this set all of its elements that are contained in the specified collection. If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.
Syntax:
public boolean removeAll(Collection> c)
Parameter: The function accepts a single parameter c
Return Value: The function returns true if this set changed as a result of the call.
Exception: The function throws the following exceptions:
Below programs illustrate the ConcurrentSkipListSet.removeAll() method:
Program 1:
// Java program to demonstrate removeAll() // method of ConcurrentSkipListSet import java.util.concurrent.*; import java.util.ArrayList; import java.util.List; class ConcurrentSkipListSetremoveAllExample1 { public static void main(String[] args) { // Initializing the List List<Integer> list = new ArrayList<Integer>(); // Adding elements in the list for (int i = 1; i <= 10; i += 2) list.add(i); // Contents of the list System.out.println("Contents of the list: " + list); // Initializing the set ConcurrentSkipListSet<Integer> set = new ConcurrentSkipListSet<Integer>(); // Adding elements in the set for (int i = 1; i <= 10; i++) set.add(i); // Contents of the set System.out.println("Contents of the set: " + set); // Remove all elements from the set which are in the list set.removeAll(list); // Contents of the set after removal System.out.println("Contents of the set after removal: " + set); } } |
Contents of the list: [1, 3, 5, 7, 9] Contents of the set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Contents of the set after removal: [2, 4, 6, 8, 10]
Program 2: Program to show NullPOinterException in removeAll().
// Java program to demonstrate removeAll() // method of ConcurrentSkipListSet import java.util.concurrent.*; import java.util.ArrayList; import java.util.List; class ConcurrentSkipListSetremoveAllExample1 { public static void main(String[] args) { // Initializing the set ConcurrentSkipListSet<Integer> set = new ConcurrentSkipListSet<Integer>(); // Adding elements in the set for (int i = 1; i <= 10; i++) set.add(i); // Contents of the set System.out.println("Contents of the set: " + set); try { // Remove all elements from the set which are null set.removeAll(null); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Contents of the set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Exception: java.lang.NullPointerException
Reference:
Recommended Posts:
- AbstractCollection removeAll() method in Java with Example
- HashSet removeAll() method in Java with Example
- Stack removeAll() method in Java with Example
- TreeSet removeAll() method in Java with Example
- ArrayDeque removeAll() method in Java
- LinkedHashSet removeAll() method in Java with Example
- Vector removeAll() Method in Java
- AbstractSequentialList removeAll() method in Java with Example
- Set removeAll() method in Java with Examples
- ConcurrentSkipListSet add() method in Java
- ConcurrentSkipListSet contains() method in Java
- ConcurrentSkipListSet first() method in Java
- ConcurrentSkipListSet last() method in Java
- LinkedTransferQueue removeAll() method in Java with Examples
- SortedSet removeAll() method in Java with Examples
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.



