Open In App

Java Collection removeIf() Method

Last Updated : 13 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The removeIf() method of Java Collection removes all the elements of the calling collection that satisfy the given predicate which is passed as a parameter to the function. If the predicate is true, then it removes the item from the calling collection otherwise it keeps the item in the collection.

Syntax for removeIf()

boolean removeIf(Predicate<? super E> filter);

Parameter: filter is a predicate that defines the condition for removing the element. It evaluates the items based on criteria and returns a boolean value.

Return Type: The removeIf() method returns a boolean value. It returns true if the calling collection has removed any element else it returns false.

Example usage of the removeIf() method

In this example, our original list contains [1,2,3,4,5] and now we want to remove all the odd numbers from the calling collection. So in the removeIf() method, we can write a predicate that returns a boolean value. So if the predicate returns true then it removes the item in the calling collection and if it returns false then it keeps the item in the collection.

So we can write the condition as if n%2!=0(if it is an odd number), then remove the element i.e. it checks every element whether it is odd or not. If it is odd then the predicate returns true and the element is removed from the calling collection. If it is even then the predicate returns false and the element is kept in the calling collection.

Below is the implementation of removeIf() method:

Java




// Java Program for demonstrating
// removeIf() method
import java.io.*;
import java.util.ArrayList;
import java.util.List;
  
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<>();
  
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
  
        // Print the original ArrayList
        System.out.println("Original List :" + list);
  
        // remove elements which are odd parity
        list.removeIf(n -> n % 2 != 0);
  
        // processed arraylist
        System.out.println("List after removeIf() method :"
                           + list);
    }
}


Output

Original List :[1, 2, 3, 4, 5]
List after removeIf() method :[2, 4]


Example 2:

In this example, We have used the removeIf() method in HashSet and removes even integers. So we have written the condition so that all even numbers get removed.

Java




// Java Program for demonstrating 
// removeIf() method
import java.util.HashSet;
import java.util.function.Predicate;
import java.io.*;
  
// Driver Class
class GFG {
      // main function
    public static void main (String[] args) {
           HashSet<Integer> hs = new HashSet<Integer>();
              
            hs.add(1);
            hs.add(2);
            hs.add(3);
            hs.add(4);
            hs.add(5);
  
              // Print the original ArrayList
            System.out.println("Original Set :"+hs);
              
            Predicate<Integer> even=n -> n % 2 == 0;
             
              //remove elements which are odd parity
            hs.removeIf(even);
  
              //processed arraylist
            System.out.println("List after removeIf() method :"+hs);    
    }
}


Output

Original Set :[1, 2, 3, 4, 5]
List after removeIf() method :[1, 3, 5]




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads