Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

removeIf()

Example

Remove all even numbers from a list:

import java.util.ArrayList;
public class Main { 
  public static void main(String[] args) {
    ArrayList<Integer> list = new ArrayList<Integer>();
numbers.add(5);
    numbers
.add(9);
    numbers.add(8);
    numbers.add(6);
    numbers.add(1);
    numbers.removeIf( n -> n % 2 == 0 );
    System.out.println(numbers);
  }
}

Definition and Usage

The removeIf() method eliminates all elements from the list for which a condition holds true. This condition is determined by the return value of a lambda expression that aligns with the test() method of Java’s Predicate interface.

Refer to our Java Lambda Expression tutorial for insights into lambda expressions.

Syntax

public boolean removeIf(Predicate condition)

Parameter Values

Parameter

Description

condition

Necessary: A Predicate object or lambda expression that evaluates an item from the list.

Technical Details

Returns:

True if any items were removed from the list; otherwise, false.