coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

ArrayList removeIf() method

In this tutorial, we will see the removeIf(Predicate filter) method of the ArrayList class in Java. This method removes all of the elements of this collection that satisfy the given predicate.

Introduction

ArrayList is a widely used class to store and retrieve data in a collection framework.

We have seen how to remove elements from the ArrayList. Today, we will see how to remove elements from the ArrayList only if it satisfy the given predicate.

Predicate is a predefined functional interface in from Java 8. It has a single abstract method test() which is used to test the given condition.

Also, we have seen a few methods of the ArrayList class before like add(), addAll(), remove(), set(), iterate(), clear(), contains(), get(), removeRange(), retainAll(), trimToSize(), indexOf() and lastIndexOf() method.

Today we will see the removeIf(Predicate filter) method.

ArrayList removeIf(Predicate filter) method

The removeIf(Predicate filter) method removes all of the elements of this collection that satisfy the given predicate.

The method signature is given below

1
public boolean removeIf(Predicate<? super E> filter)

As shown in the method signature, this method accepts only one parameter filter. It is an instance of the Predicate Functional Interface.

A predicate is a predefined functional Interface used to test the conditions.

This method returns a boolean value. This method returns true if any elements from the ArrayList are removed. Otherwise, it returns false.

Let’s see an example Java program for the ArrayList removeIf() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * A Java program showing an example for the removeIf()
 * method of the ArrayList class in Java.
 * 
 * @author coderolls.com
 */
public class ArrayListRemoveIfExample {

  public static void main(String[] args) {
    // create an empty ArrayList object 'states'
    List<String> states = new ArrayList<String>();
    
    // add state in the ArrayList, Florida multiple times
    states.add("California");
    states.add("Texas");
    states.add("Florida");
    states.add("Florida");
    states.add("New Jersey");
    states.add("Washington");
    states.add("Florida");
    
    System.out.println("The states list before the removeIf() call: \n" + states);
    
    // a predicate for the condition
    Predicate p = s -> s.equals("Florida");
    states.removeIf(p); // removes all elements which satisfy the predicate p
    
    System.out.println("\nThe states list after the removeIf() call: \n" + states);
  }
}

Output:

1
2
3
4
5
The states list before the removeIf() call: 
[California, Texas, Florida, Florida, New Jersey, Washington, Florida]

The states list after the removeIf() call: 
[California, Texas, New Jersey, Washington]

Conclusion

The removeIf(Predicate filter) method removes all of the elements of the ArrayList that satisfy the given predicate.

1
public boolean removeIf(Predicate<? super E> filter)

The example Java program used in the above article can be found at this GitHub repository.

Please write your thoughts in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus