CopyOnWriteArraySet removeIf() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

CopyOnWriteArraySet: [10, 20, 30, 40, 50, 60, 70, 80, 90]
Updated CopyOnWriteArraySet: [10, 20, 40, 50, 70, 80]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

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


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.