The Wayback Machine - http://web.archive.org/web/20240628105031/https://www.geeksforgeeks.org/arraylist-removeif-method-in-java/
Open In App

ArrayList removeIf() method in Java

Last Updated : 09 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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)



Previous Article
Next Article

Similar Reads

Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition che
3 min read
Vector removeIf() method in Java
The removeIf() method of Vector removes all of those elements from Vector which satisfies the condition passed as a parameter to this method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition checking function, which checks the gi
3 min read
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&lt;E&gt; filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based on which elements are removed from this set. Return
2 min read
LinkedTransferQueue removeIf() method in Java with Examples
The removeIf() method of java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to remove all of the elements of this queue that satisfies a given predicate filter which is passed as a parameter to the method. Syntax: public boolean removeIf(Predicate filter) Parameters: This method takes a parameter filter which rep
2 min read
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&lt;E&gt; filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based on which elements are removed from this List. Return Valu
2 min read
LinkedBlockingDeque removeIf() method in Java with Examples
The removeIf() method of LinkedBlockingDeque removes the element from this LinkedBlockingDeque that satisfies the specified condition. Syntax: public boolean removeIf (Predicate&lt;? super E&gt; filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based on which elements are removed from this Deque. Retu
2 min read
Java Collection removeIf() Method
The removeIf() method of Java Collection removes all the elements of the calling collection that satisfy the given predicate which is passed as a parameter to the function. If the predicate is true, then it removes the item from the calling collection otherwise it keeps the item in the collection. Syntax for removeIf()boolean removeIf(Predicate&lt;
3 min read
Java.util.ArrayList.add() Method in Java
Below are the add() methods of ArrayList in Java: boolean add(Object o) : This method appends the specified element to the end of this list. Parameters: object o: The element to be appended to this list. Exception: NA // Java code to illustrate add(Object o) import java.io.*; import java.util.ArrayList; public class ArrayListDemo { public static vo
2 min read
Java.util.ArrayList.addall() method in Java
Below are the addAll() methods of ArrayList in Java: boolean addAll(Collection c) : This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the ope
4 min read
Java.util.Arraylist.indexOf() in Java
The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Syntax : public int IndexOf(Object o) obj : The element to search for. // Java code to demonstrate the working of // indexOf in ArrayList // for ArrayList functions import java.util.Arra
2 min read
three90RightbarBannerImg