Open In App
Related Articles

CopyOnWriteArraySet removeIf() method in Java with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
The removeIf() method of CopyonWriteArraySet method removes the element from this CopyOnWriteArraySet that satisfies the specified condition. Syntax:
public boolean removeIf (Predicate<E> filter)
Parameters: This method accepts a mandatory parameter filter which is the predicate value based on which elements are removed from this set. Return Value: This method returns a boolean value such as true, if the CopyOnWriteArraySet is changed. Else this method returns false. Exceptions: This method throws NullPointerException if the specified Predicate filter is null. Below program illustrates the removeIf() function of CopyOnWriteArrayList class : Program 1:
// Java Program to illustrate the CopyOnWriteArraySet
// removeIf() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet<Integer> ArrSet
            = new CopyOnWriteArraySet<Integer>();
  
        // Add elements
        ArrSet.add(10);
        ArrSet.add(20);
        ArrSet.add(30);
        ArrSet.add(40);
        ArrSet.add(50);
        ArrSet.add(60);
        ArrSet.add(70);
        ArrSet.add(80);
        ArrSet.add(90);
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        // if a number in the set is
        // divisible by 3, then remove it
        ArrSet.removeIf(number -> number % 3 == 0);
  
        // print updated CopyOnWriteArraySet
        System.out.println("Updated CopyOnWriteArraySet: "
                           + ArrSet);
    }
}

                    
Output:
CopyOnWriteArraySet: [10, 20, 30, 40, 50, 60, 70, 80, 90]
Updated CopyOnWriteArraySet: [10, 20, 40, 50, 70, 80]
Program 2:
// Java Program to illustrate the CopyOnWriteArraySet
// removeIf() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet<String> ArrSet
            = new CopyOnWriteArraySet<String>();
  
        // Add elements
        ArrSet.add("GeeksforGeeks");
        ArrSet.add("GFG");
        ArrSet.add("Geeks");
        ArrSet.add("Gfg");
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        try {
  
            // if a number in the set is
            // divisible by 3, then remove it
            // This will throw NullPointerException
            ArrSet.removeIf(null);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

                    
Output:
CopyOnWriteArraySet: [GeeksforGeeks, GFG, Geeks, Gfg]
java.lang.NullPointerException


Last Updated : 28 Jan, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads