- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
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
Removing Elements from an ArrayList of Integers based on Condition Example
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]
Removing Elements from an ArrayList of Strings based on Condition Example
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]
Removing Elements from an ArrayList of Objects based on Condition Example
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 ]]