Java LinkedList removeAll() Method with Examples

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

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

Java LinkedList removeAll() Method with Examples

This java.util.LinkedList.removeAll() method is used to remove all the elements from the LinkedList or in the specified collection. It returns true if the original list is changed after calling removeAll() method.

Let’s see removeAll() method with suitable examples.

Method-1: Remove All Elements From The LinkedList

Syntax:

LinkedListName.removeAll(Collection c)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • Collection c refers to a collection c whose all the elements will be removed.

Approach:

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Remove all the element from the LinkedList using removeAll( ) method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l1 = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l1.add(1);
        l1.add(2);
        l1.add(3);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList 1 are: "+l1);
        // removing all the element from the LinkedList
        l1.removeAll(l1);
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l1);
    }
}
Output:

The elements of LinkedList 1 are: [1, 2, 3]
The new elements of LinkedList are: []

Method-2: Remove All Elements From an LinkedList Present in Another LinkedList

Syntax:

LinkedListName.removeAll(Collection c)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • Collection c refers to a collection c whose all the elements will be removed.

Apporach:

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the first LinkedList i.e. l1 using the add() method.
  • Display the LinkedList elements.
  • Add another new elements to the new LinkedList i.e. l2 using add( ) method.
  • Display the LinkedList elements.
  • Remove all the element from the first LinkedList which are present in second LinkedList using removeAll( ) method.
  • Print the first LinkedList i.e. l1 after removal of elements.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l1 = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l1.add(1);
        l1.add(2);
        l1.add(3);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList 1 are: "+l1);
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l2 = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l2.add(4);
        l2.add(2);
        l2.add(3);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList 2 are: "+l2);
        // removing all the element from the LinkedList
        l1.removeAll(l2);
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l1);
    }
}
Output:

The elements of LinkedList 1 are: [1, 2, 3]
The elements of LinkedList 2 are: [4, 2, 3]
The new elements of LinkedList are: [1]

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: