Java LinkedList removeIf() Method with Examples

In the previous article, we have discussed about Java LinkedList removeAll() Method with Examples

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

Java LinkedList removeIf() Method with Examples

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

Syntax:

LinkedListName.removeIf()

Where,

  • LinkedListName refers to the name of your LinkedList.

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

Example-1: removeIf() method and condition with contains() method

Approach:

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

Program:

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

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

Example-2: removeIf() method and condition with endsWith() method

Approach: 

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

Program:

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

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

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: