Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList removeIf() Method

Posted on July 24, 2025July 24, 2025 By Admin No Comments on Java ArrayList removeIf() Method

The removeIf() method is a powerful method in the Java ArrayList class that lets you remove all elements that satisfy a specified condition—provided as a functional predicate or lambda expression. This approach is especially useful when you want to filter out multiple elements at once based on a dynamic rule, all in a single, concise statement.

What Is the removeIf() Method in ArrayList?

  • The removeIf() method removes every element from the list for which the given condition (predicate) returns true.
  • The condition is specified via a lambda expression or any implementation of the Predicate functional interface.
  • The method operates in-place, modifying the original ArrayList directly.
  • Returns true if any elements were removed; otherwise, returns false.

Syntax

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

Where:

  • filter is a predicate (lambda or method reference) that returns true for elements to be removed.
  • E is the type of elements in the list.

Parameters

ParameterDescription
filterA Predicate (condition) that returns true for items to remove

Return Value

ReturnsDescription
trueIf one or more elements were removed
falseIf no elements were removed

Exceptions

  • Throws NullPointerException if the specified predicate is null.
  • Any runtime exception thrown by the predicate during iteration will be propagated to the caller.

How Does removeIf() Work Internally?

  • Iterates over all elements in the list.
  • Evaluates each element with the given predicate.
  • Removes elements for which the predicate returns true.
  • Shifts subsequent elements to fill removed positions, then updates the size of the list.

Examples of the removeIf() Method

1. Remove All Even Numbers from an ArrayList

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);
        numbers.add(6);

        // Remove all even numbers
        numbers.removeIf(n -> n % 2 == 0);

        System.out.println(numbers); // Output: [1, 3, 5]
    }
}

In this example, every even number is removed using a lambda expression as a predicate.

2. Remove Strings Containing a Specific Substring

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> countries = new ArrayList<>();
        countries.add("Iceland");
        countries.add("America");
        countries.add("Ireland");
        countries.add("Canada");
        countries.add("Greenland");

        // Remove all countries containing "land"
        countries.removeIf(str -> str.contains("land"));

        System.out.println(countries); // Output: [America, Canada]
    }
}

Removes every country whose name contains “land”, leaving only others.

3. Remove All Elements Divisible by 3

import java.util.ArrayList;

public class Example {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(23);
        numbers.add(32);
        numbers.add(45);
        numbers.add(63);

        // Remove numbers that are divisible by 3
        numbers.removeIf(n -> n % 3 == 0);

        System.out.println(numbers); // Output: [23, 32]
    }
}

Filters out all elements for which the condition is true (divisible by 3).

4. Remove Objects Based on Custom Field

import java.util.ArrayList;

class Employee {
    String name;
    Employee(String name) { this.name = name; }
    public String toString() { return name; }
}

public class Demo {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("Alex"));
        employees.add(new Employee("Brian"));
        employees.add(new Employee("Pranav"));
        employees.add(new Employee("Pradeep"));

        // Remove employees whose names start with 'P'
        employees.removeIf(emp -> emp.name.startsWith("P"));

        System.out.println(employees); // Output: [Alex, Brian]
    }
}

Removes all employees whose names start with the letter ‘P’.

Important Notes

  • Efficient bulk removal: Removes many elements matching a predicate in one call—no need for manual loop and removal.
  • Lambda expressions recommended: Clean and concise with Java 8+ features.
  • Predicate flexibility: You can provide any complex logic as your removal condition.
  • Null predicate: Throws NullPointerException if the predicate is null.
  • Original list is modified: All matching elements are removed from the source list.
  • Works for all data types: You can use it with lists of integers, strings, custom objects, etc.
  • Order is preserved: Non-matching elements remain, with their order retained.

Summary

AspectDetails
Method PurposeRemove every element matching a condition
Syntaxlist.removeIf(Predicate<? super E> filter)
Return Valuetrue if any item was removed, false otherwise
PredicateCondition as a lambda or predicate
ExceptionsNullPointerException if predicate is null
Typical Use CasesFiltering lists, batch deletion, advanced cleanup

Typical Use Cases

  • Filtering out unwanted data: Remove all entries that meet a specific criterion quickly.
  • Batch cleanup: Delete items based on validation rules or other business logic.
  • Dynamic data preparation: Prepare lists for output or processing by removing irrelevant entries.
Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList removeAll() Method
Next Post: Java ArrayList replaceAll() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList remove() Method
  • Java ArrayList removeAll() Method
  • Java ArrayList removeIf() Method
  • Java ArrayList replaceAll() Method
  • Java ArrayList retainAll() Method
  • Java ArrayList set() Method
  • Java ArrayList size() Method
  • Java ArrayList sort() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList subList() Method
  • Java ArrayList toArray() Method
  • Java ArrayList trimToSize() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme