Java ArrayList removeIf() Method with Example

In the previous article we have discussed about Java ArrayList forEach() Method with Example

In this article we are going to see the use Java ArrayList removeIf() method along with suitable examples.

Java ArrayList removeIf() Method with Example

removeIf():

This java.util.ArrayList.removeIf() method is used to remove all the elements that satisfies the specified condition within the ArrayList.

Syntax:

arrayListName.removeIf()

Where,

  • arrayListName refers to the name of your ArrayList.

Let’s see different examples to understand it more clearly.

Method-1: Java ArrayList removeIf() Method – Example with String Type ArrayList

Here we have used inbuilt contains() method to check if the element contains that specific string.

Approach:

  • Create a new ArrayList of type String.
  • Add string elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Now, using the removeIf() method we will remove all the elements which contains “ap”
  • Print the new arrayList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<String> arr1 = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr1.add("apple");
        arr1.add("banana");
        arr1.add("apricot");
        arr1.add("guava");
        arr1.add("custard apple");
        // Prints the ArrayList elements
        System.out.println("The elements in the ArrayList are: "+arr1);
        // remove the elements contains “ap”
       arr1.removeIf(e -> e.contains("ap"));
       System.out.println("Elements without ‘ap’: " + arr1);
    }
}
Output:

The elements in the ArrayList are: [apple, banana, apricot, guava, custard apple]
Elements without ‘ap’: [banana, guava]

Method-2: Java ArrayList removeIf() Method – Example with String Type ArrayList

Here we have used inbuilt endsWith() method to check if the element contains that specific string at its end.

Approach:

  • Create a new ArrayList of type String.
  • Add string elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Now, using the removeIf() method we will remove all the elements which which ends with “e”
  • Print the new arrayList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<String> arr1 = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr1.add("apple");
        arr1.add("banana");
        arr1.add("apricot");
        arr1.add("guava");
        arr1.add("custard apple");
        // Prints the ArrayList elements
        System.out.println("The elements in the ArrayList are: "+arr1);
        // remove the elements which ends with “e”
       arr1.removeIf(e -> e.endsWith("e"));
       System.out.println("Remove elements ends with ‘e’: " + arr1);
    }
}
Output:

The elements in the ArrayList are: [apple, banana, apricot, guava, custard apple]
Remove elements ends with ‘e’: [banana, apricot, guava]

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: