Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

ArrayList removeIf(): Remove Elements Matching a Condition

Java ArrayList.removeIf() removes all elements that satisfy a condition by iterating through the elements and matching against the specified Predicate.

Lokesh Gupta

August 7, 2023

Java ArrayList
Java ArrayList
ArrayList

Java ArrayList.removeIf() method removes all elements that satisfy a condition by iterating through the elements of the current arraylist and matching them against the condition specified by the argument Predicate.

ArrayList<String> arraylist = new ArrayList<>(Arrays.asList("A", "B", "C", "C", "D"));

arraylist.removeIf(e -> e.equals("C"));   //[A, B, D]

Note that the ArrayList class also provides other methods for removing elements such as:

  • remove(): it removes a single element by value or index position.
  • removeAll(): it removes all occurrences of specified elements by their value.

1. Syntax

The removeIf() takes a single argument of type Predicate. The Predicate interface is a functional interface that represents a condition (boolean-valued function) of one argument. It checks that is a given argument met the condition or not.

public boolean removeIf(Predicate<? super E> filter);
  • Method parameter – filter predicate that returns true for elements to be removed.
  • Method returns – true if any elements were removed from this list.
  • Method throws – NullPointerException if predicate is null.

2. Examples to Remove Elements Matching a Condition

For removing the elements from the arraylist, we can create the conditions in multiple ways using the Predicate instances. Let us see a few usecases.

2.1. Remove All Even Numbers from a List of Numbers

In this simple example, we have a list of odd and even numbers. Then we use the removeIf() method to remove all even numbers from the list.

ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

numbers.removeIf(number -> number % 2 == 0);

System.out.println(numbers);

Program output.

[1, 3, 5, 7, 9]

2.2. Remove All Objects for Matching Field Value

In another example, we have a list of employees. We are removing all employees whose names start with the character 'P'.

The predicate, employee.getName().startsWith(“P”) matches the name of each Employee and returns true if the name starts with P.

ArrayList<Employee> employees = new ArrayList<>();

employees.add(new Employee(1l, "Alex", LocalDate.of(2018, Month.APRIL, 21)));
employees.add(new Employee(4l, "Brian", LocalDate.of(2018, Month.APRIL, 22)));
employees.add(new Employee(3l, "Piyush", LocalDate.of(2018, Month.APRIL, 25)));
employees.add(new Employee(5l, "Charles", LocalDate.of(2018, Month.APRIL, 23)));
employees.add(new Employee(2l, "Pawan", LocalDate.of(2018, Month.APRIL, 24)));

Predicate<Employee> condition = employee -> employee.getName().startsWith("P");

employees.removeIf(condition);

System.out.println(employees);

Program output.

[
	Employee [id=1, name=Alex, dob=2018-04-21],
	Employee [id=4, name=Brian, dob=2018-04-22],
	Employee [id=5, name=Charles, dob=2018-04-23]
]

3. Conclusion

The removeIf() method is a very convenient way to remove multiple elements from the arraylist that match a certain condition. Combined with the power of predicates and functional programming, this method helps in creating concise and readable code for such usecases.

That’s all for the ArrayList removeIf() in Java.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

ArrayList Methods

  • ArrayList add()
  • ArrayList addAll()
  • ArrayList clone()
  • ArrayList contains()
  • ArrayList ensureCapacity()
  • ArrayList forEach()
  • ArrayList get()
  • Arraylist indexOf()
  • Arraylist lastIndexOf()
  • ArrayList listIterator()
  • ArrayList remove()
  • ArrayList removeAll()
  • ArrayList removeIf()
  • ArrayList retainAll()
  • ArrayList spliterator()
  • ArrayList subList()
  • ArrayList toArray()

ArrayList Examples

  • Initialize Arraylist
  • Iteration
  • Add/replace Element
  • Add Multiple Elements
  • Check Empty List
  • Remove Element
  • Replace Element
  • Empty ArrayList
  • Synchronized ArrayList
  • Compare two lists
  • Remove duplicates
  • Merge two lists
  • Serialize a List
  • Swap two elements
  • Convert Array to ArrayList
  • Convert HashSet to ArrayList
  • Convert LinkedList to ArrayList
  • ArrayList vs LinkedList
  • ArrayList vs Vector

Table of Contents

  • 1. Syntax
  • 2. Examples to Remove Elements Matching a Condition
    • 2.1. Remove All Even Numbers from a List of Numbers
    • 2.2. Remove All Objects for Matching Field Value
  • 3. Conclusion
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java ArrayList replaceAll()

Next

Java ArrayList spliterator() Example

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap