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

CopyOnWriteArrayList removeIf() method in Java with Examples

The removeIf() method of CopyOnWriteArrayList removes the element from this CopyOnWriteArrayList 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 List.

Return Value: This method returns a boolean value such as true, if the CopyOnWriteArrayList 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 :




// Java Program to illustrate the CopyOnWriteArrayList
// removeIf() method in Java
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<Integer> ArrLis
            = new CopyOnWriteArrayList<Integer>();
  
        // Add elements
        ArrLis.add(2);
        ArrLis.add(3);
        ArrLis.add(4);
        ArrLis.add(7);
        ArrLis.add(6);
        ArrLis.add(9);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: "
                           + ArrLis);
  
        // if a number in the List is
        // divisible by 3, then remove it
        ArrLis.removeIf(number -> number % 3 == 0);
  
        // print updated CopyOnWriteArrayList
        System.out.println("Updated CopyOnWriteArrayList: "
                           + ArrLis);
    }
}


Output:

CopyOnWriteArrayList: [2, 3, 4, 7, 6, 9]
Updated CopyOnWriteArrayList: [2, 4, 7]




// Java Program to illustrate the CopyOnWriteArrayList
// removeIf() method in Java
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<String> ArrLis
            = new CopyOnWriteArrayList<String>();
  
        // Add elements
        ArrLis.add("Lazyroar");
        ArrLis.add("GFG");
        ArrLis.add("Geeks");
        ArrLis.add("Gfg");
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: "
                           + ArrLis);
  
        try {
  
            // This will throw NullPointerException
            ArrLis.removeIf(null);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

CopyOnWriteArrayList: [Lazyroar, GFG, Geeks, Gfg]
java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#removeIf-java.util.function.Predicate-

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