Java LinkedList retainAll() Method with Examples

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

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

Java LinkedList retainAll() Method with Examples

This java.util.LinkedList.retainAll() method is used to retrieve only the common elements from the 2 collections.

It returns true if all the element of one collection is retrieved from another collection else it returns false.

If the type of collection is incompatible with another collection then it throws ClassCastException.

Syntax:

LinkedLisName.tretainAll(Collection c)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • Collection c refers to retain all the elements of collection c.

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

Method-1: Java LinkedList retainAll() Method – Example with String Type LinkedList

Approach:

  • Create 2 new LinkedList of type String.
  • Add string elements into both LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then retrieve one LinkedList from the another LinkedList using retainAll() method.
  • Print the elements.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("mi");
        l.add("vivo");
        l.add("samsung");
        l.add("nokia");
        l.add("black berry");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList 1 are: "+l);
        // Create a LinkedList of string datatype
        LinkedList<String> l1 = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l1.add("vivo");
        l1.add("htc");
        l1.add("samsung");
        l1.add("realme");
        l1.add("nokia");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList 2 are: "+l1);
        // retrive the common elements
        l.retainAll(l1);
        System.out.println("common elements in List 1 are: " +l);
        l1.retainAll(l);
        System.out.println("common elements in List 2 are: " +l1);
    }
}

Output:

The elements of LinkedList 1 are: [mi, vivo, samsung, nokia, black berry]
The elements of LinkedList 2 are: [vivo, htc, samsung, realme, nokia]
common elements in List 1 are: [vivo, samsung, nokia]
common elements in List 2 are: [vivo, samsung, nokia]

Method-2: Java LinkedList retainAll() Method – Example with Integer Type LinkedList

Approach:

  • Create 2 new LinkedList of type Integer.
  • Add integer elements into both LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then retrieve one LinkedList from the another LinkedList using retainAll() method.
  • Print the elements.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        l.add(1);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList 1 are: "+l);
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l1 = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l1.add(1);
        l1.add(100);
        l1.add(84);
        l1.add(17);
        l1.add(0);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList 2 are: "+l1);
        // retrive the common elements
        l.retainAll(l1);
        System.out.println("common elements in List 1 are: " +l);
        l1.retainAll(l);
        System.out.println("common elements in List 2 are: " +l1);
    }
}
Output:

The elements of LinkedList 1 are: [2, 52, 13, 17, 1]
The elements of LinkedList 2 are: [1, 100, 84, 17, 0]
common elements in List 1 are: [17, 1]
common elements in List 2 are: [1, 17]

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.

Related Java Programs: