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

Eliminate all even numbers from the list.

import java.util.LinkedList;
 
public class Main {
  public static void main(String[] args) {
    LinkedList<Integer> numbers = new LinkedList<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 removes all elements from
the list that satisfy a specified condition. This condition can be expressed
through a lambda expression compatible with Java’s
Predicate interface’s test() method.

Syntax

public boolean removeIf(Predicate condition)

Parameter Values

Parameter

Description

condition

Mandatory: 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.