Skip to content
Related Articles

Related Articles

ArrayList removeIf() method in Java
  • Last Updated : 09 Sep, 2019

The removeIf() method of ArrayList is used to remove all of the elements of this ArrayList that satisfies a given predicate filter which is passed as a parameter to the method.

Errors or runtime exceptions are thrown during iteration or by the predicate are pass to the caller. This method returns True, if we are able to remove some element.

Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition checking function, check the given input for a given condition and returns a boolean result for the same indicating whether the condition was met or not.

Java 8 Predicate with Examples.

Syntax:



public boolean removeIf(Predicate filter)

Parameter: This method takes a parameter filter which represents a predicate which returns true for elements to be removed.

Returns: This method returns True if predicate returns true and we are able to remove elements.

Exception: This method throws NullPointerException if the specified filter is null.

Below programs illustrate removeIf() method of ArrayList:

Program 1: Program to demonstrate removeIf() method on ArrayList which contains a set of Numbers and only the numbers which are divisible by 3 will be removed.




// Java Program Demonstrate removeIf()
// method of ArrayList
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an ArrayList which going to
        // contains a list of Numbers
        ArrayList<Integer> Numbers = new ArrayList<Integer>();
  
        // Add Number to list
        Numbers.add(23);
        Numbers.add(32);
        Numbers.add(45);
        Numbers.add(63);
  
        // apply removeIf() method
        // we are going to remove numbers divisible by 3
        Numbers.removeIf(n -> (n % 3 == 0));
  
        // print list
        for (int i : Numbers) {
            System.out.println(i);
        }
    }
}
Output:
23
32

Program 2: Program to demonstrate removeIf() method on ArrayList which contains set of Students Names and names starting with “S” will be removed.




// Java Program Demonstrate removeIf()
// method of ArrayList
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // create an ArrayList which going to
        // contains a list of Student names which is actually
        // string values
        ArrayList<String> students = new ArrayList<String>();
  
        // Add Strings to list
        // each string represents student name
        students.add("Ram");
        students.add("Mohan");
        students.add("Sohan");
        students.add("Rabi");
        students.add("Shabbir");
  
        // apply removeIf() method
        // we are going to remove names
        // start with S
        students.removeIf(n -> (n.charAt(0) == 'S'));
  
        System.out.println("Students name Does not start with S");
        // print list
        for (String str : students) {
            System.out.println(str);
        }
    }
}
Output:
Students name Does not start with S
Ram
Mohan
Rabi

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/ArrayList.html#removeIf(java.util.function.Predicate)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :