Java LinkedList remove(), removeFirst(), removeLast(), removeIf() and clear() Example

Java LinkedList Examples


In this post, we will learn how to remove elements from LinkedList using the following APIs:
  • removeFirst()
  • removeLast()
  • remove(Object o)
  • removeIf(Predicate<? super String> filter)
  • clear()
Let's first create LinkedList with few fruits and then use remove methods to remove fruits from LinkedList.
LinkedList<String> fruitList = new LinkedList<>();

fruitList.add("Apple");
fruitList.add("banana");
fruitList.add("mango");
fruitList.add("Pinaple");

System.out.println("Initial LinkedList = " + fruitList);

removeFirst()

Remove the first element in the LinkedList. Throws NoSuchElementException if the LinkedList is empty.
String element = fruitList.removeFirst();   
System.out.println("Removed the first element " + element + " => " + fruitList);

removeLast()

Remove the last element in the LinkedList. Throws NoSuchElementException if the LinkedList is empty
element = fruitList.removeLast();    
System.out.println("Removed the last element " + element + " => " + fruitList);

remove(Object o)

Remove the first occurrence of the specified element from the LinkedList
boolean isRemoved = fruitList.remove("banana");
if(isRemoved) {
 System.out.println("Removed banana => " + fruitList);
}

removeIf(Predicate<? super String> filter)

Removes all of the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during the iteration or by the predicate are relayed to the caller.
fruitList.removeIf(programmingLanguage -> programmingLanguage.startsWith("C"));
System.out.println("Removed elements starting with C => " + fruitList);

clear()

Removes all of the elements from this list. The list will be empty after this call returns.
fruitList.clear();
System.out.println("Cleared the LinkedList => " + fruitList);

Complete Example

package com.javaguides.collections.linkedlistexamples;

import java.util.LinkedList;

public class RemoveElementsFromLinkedListExample {
    public static void main(String[] args) {
        LinkedList < String > fruitList = new LinkedList < > ();

        fruitList.add("Apple");
        fruitList.add("banana");
        fruitList.add("mango");
        fruitList.add("Pinaple");

        System.out.println("Initial LinkedList = " + fruitList);

        // Remove the first element in the LinkedList
        String element = fruitList.removeFirst(); // Throws NoSuchElementException if the LinkedList is empty
        System.out.println("Removed the first element " + element + " => " + fruitList);

        // Remove the last element in the LinkedList
        element = fruitList.removeLast(); // Throws NoSuchElementException if the LinkedList is empty
        System.out.println("Removed the last element " + element + " => " + fruitList);

        // Remove the first occurrence of the specified element from the LinkedList
        boolean isRemoved = fruitList.remove("C#");
        if (isRemoved) {
            System.out.println("Removed C# => " + fruitList);
        }

        // Remove all the elements that satisfy the given predicate
        fruitList.removeIf(programmingLanguage - > programmingLanguage.startsWith("C"));
        System.out.println("Removed elements starting with C => " + fruitList);

        // Clear the LinkedList by removing all elements
        fruitList.clear();
        System.out.println("Cleared the LinkedList => " + fruitList);
    }
}

Output

Initial LinkedList = [Apple, banana, mango, Pinaple]
Removed the first element Apple => [banana, mango, Pinaple]
Removed the last element Pinaple => [banana, mango]
Removed elements starting with C => [banana, mango]
Cleared the LinkedList => []



Comments