Wednesday, July 3, 2024
HomeLanguagesJavaCopyOnWriteArraySet removeIf() method in Java with Examples

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:




// 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("Lazyroar");
        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: [Lazyroar, GFG, Geeks, Gfg]
java.lang.NullPointerException

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments