Java ArrayList removeIf() Method


Description

The Java ArrayList removeIf(Predicate<? super E> filter) method retrieves and removes all the elements of this ArrayList that satisfy the given predicate. In case of exception, the exception is relayed to the caller.

Declaration

Following is the declaration for java.util.ArrayList.removeIf(filter) method

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

Parameters

  • filter − a predicate which returns true for elements to be removed.

Return Value

true if any elements were removed.

Exception

NullPointerException − if the specified filter is null

Example 1

The following example shows the usage of Java ArrayList removeIf(filter) method. We're creating an ArrayList of Integers, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As ArrayList is modified it is printed to check if even numbers are removed or not.

package com.tutorialspoint;
import java.util.ArrayList;
public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      ArrayList<Integer> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(5);

      // let us print all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);

      // it will remove even numbers from the arrayList
      arrayList.removeIf(i -> i%2 == 0);
	  
      // let us print all the elements available in arrayList again
      System.out.println("ArrayList = " + arrayList);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

ArrayList = [1, 2, 3, 4, 5]
ArrayList = [1, 3, 5]

Example 2

The following example shows the usage of Java ArrayList removeIf(filter) method. We're creating an ArrayList of String, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As ArrayList is modified it is printed to check if even numbers are removed or not.

package com.tutorialspoint;
import java.util.ArrayList;
public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      ArrayList<String> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add("A");
      arrayList.add("BB");
      arrayList.add("C");
      arrayList.add("DD");
	   arrayList.add("E");

      // let us print all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);

      // it will remove single length string
      arrayList.removeIf(i -> i.length() == 1);
	  
      // let us print all the elements available in arrayList again
      System.out.println("ArrayList = " + arrayList);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

ArrayList = [A, BB, C, DD, E]
ArrayList = [BB, DD]

Example 3

The following example shows the usage of Java ArrayList removeIf(filter) method. We're creating an ArrayList of Student objects, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As ArrayList is modified it is printed to check if even numbers are removed or not.

package com.tutorialspoint;
import java.util.ArrayList;
public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      ArrayList<Student> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(new Student(1, "Julie"));
      arrayList.add(new Student(2, "Robert"));
      arrayList.add(new Student(3, "Adam"));
	   arrayList.add(new Student(4, "Jene"));
	   arrayList.add(new Student(5, "Jacob"));
	  
	   // let us print all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);

      // it will remove if roll no is 2
      arrayList .removeIf(i -> i.rollNo == 2);
	  
      // let us print all the elements available in arrayList again
      System.out.println("ArrayList = " + arrayList);
   }
}
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

ArrayList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Jene ], [ 5, Jacob ]]
ArrayList = [[ 1, Julie ], [ 3, Adam ], [ 4, Jene ], [ 5, Jacob ]]
java_util_arraylist.htm
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements