Java LinkedList containsAll() Method with Examples

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

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

Java LinkedList containsAll() Method with Examples

This java.util.LinkedList.containsAll() method is used to check if all the elements are present in the LinkedList  or not. It returns true if all the elements are present else it returns false.

Syntax:

LinkedListName.containsAll()

Where,

  • LinkedListName refers to the name of your LinkedList.

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

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

Approach:

  • Create two LinkedLists of type String.
  • Add string elements into both LinkedLists using the add() method.
  • Display both LinkedList elements.
  • Now, using the containsAll() method check if all the elements of one LinkedList is present in another LinkedList or not.
  • Print the result as true/false.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l1 = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l1.add("vivo");
        l1.add("samsung");
        l1.add("nokia");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList 1 are: "+l1);
        // Create a LinkedList of string datatype
        LinkedList<String> l2 = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l2.add("vivo");
        l2.add("htc");
        l2.add("samsung");
        l2.add("realme");
        l2.add("nokia");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList 2 are: "+l2);
        // check if it contains all the elements or not
        System.out.println("are all the elements of LinkedList 1 present in LinkedList 2: " +l2.containsAll(l1));
        System.out.println("are all the elements of LinkedList 2 present in LinkedList 1: " +l1.containsAll(l2));
    }
}
Output:

The elements of LinkedList 1 are: [vivo, samsung, nokia]
The elements of LinkedList 2 are: [vivo, htc, samsung, realme, nokia]
are all the elements of LinkedList 1 present in LinkedList 2: true
are all the elements of LinkedList 2 present in LinkedList 1: false

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

Approach:

  • Create two LinkedLists of type Integer.
  • Add integer elements into both LinkedLists using the add() method.
  • Display both LinkedList elements.
  • Now, using the containsAll() method check if all the elements of one LinkedList is present in another LinkedList or not.
  • Print the result as true/false.

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(17);
        l1.add(1);
        // 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(1);
        l2.add(100);
        l2.add(84);
        l2.add(17);
        l2.add(0);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList 2 are: "+l2);
        // check if it contains all the elements or not
        System.out.println("are all the elements of LinkedList 1 present in LinkedList 2: " +l2.containsAll(l1));
        System.out.println("are all the elements of LinkedList 2 present in LinkedList 1: " +l1.containsAll(l2));
    }
}

Output:

The elements of LinkedList 1 are: [17, 1]
The elements of LinkedList 2 are: [1, 100, 84, 17, 0]
are all the elements of LinkedList 1 present in LinkedList 2: true
are all the elements of LinkedList 2 present in LinkedList 1: false

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: