ArrayList removeIf Method Tutorial

boolean removeif(Predicate<? super e> filter)

The removeIf method removes all matching elements in the current ArrayList instance that evaluate to true for the Predicate expression filter (say what??? - I'll explain). In order to use this method, you will need to have a basic understanding of lambda expressions. If you are not familiar with lambda expressions, the following tutorials will teach you what you need to know in order to use this method:

The Predicate interface is a functional interface that has a single abstract method named test that returns a primitive boolean result.
public interface Predicate<T> {
      boolean test(T t);
So basically we can pass a Predicate based lambda expression as the argument for this method to do its thing. I'll demonstrate exactly how the Predicate interface works in the source code.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md ArrayListRemoveIf
C:\Java>cd ArrayListRemoveIf

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

import java.util.*;
import java.util.function.Predicate;

class ArrayListRemoveIf {
    public static void main(String args[]) {

        FunctionalInterface<Integer> fi;
        fi = (x) -> x < 5 ;
        int counter = 0;
        while(fi.test(counter)) {
            System.out.println("Counter = " + counter);

        counter = 0;
        Predicate<Integer> pi = (x) -> x < 5;
        while(pi.test(counter)) {
            System.out.println("Counter = " + counter);
        ArrayList<String> states = new ArrayList<String>();
        states.addAll(Arrays.asList("Alaska", "Alabama", "California", "Colorado", "Hawaii", "New York"));
        System.out.println("states = " + states);
        Predicate<String> p = (x) -> x.substring(0, 1).equals("A");
        System.out.println("states.removeIf(p) = " + states);

        states.removeIf( (String x) -> x.substring(0, 1).equals("C") );
        System.out.println("Now states = " + states);


interface FunctionalInterface<T> {
    boolean test(T t) ;

Now switch back to the command prompt (CMD) and type in javac and press Enter.
Now type in java ArrayListRemoveIf and press Enter.

C:\Java\ArrayListRemoveIf>java ArrayListRemoveIf

Final thoughts