Java Stream How to - Remove element from List with removeIf method








Question

We would like to know how to remove element from List with removeIf method.

Answer

import java.util.ArrayList;
import java.util.List;
/*from  w ww.  ja  v  a 2  s  .  c  om*/
public class Main {
  
  public static void main(String[] args) {
    List<Integer> l = new ArrayList<Integer>();
    l.add(3);
    l.add(2);
    l.add(1);
    l.add(4);
    l.removeIf( i -> {
      return i >= 3;//No return statement will break compilation
    });
    System.out.println(l);
  }
  
}

The code above generates the following result.