Java LinkedList addAll() Method with Examples

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

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

Java LinkedList addAll() Method with Examples

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

addAll(Collection col)

This java.util.LinkedList.addAll(Collection col) method adds/insert all the element of a collection to the last of LinkedList.

It returns True if it successfully adds all the elements of collection to the LinkedList.

NullPointerException will come if the collection that you want to add does not have any element in it.

Syntax:

LinkedListName.addAll(Collection col)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • Collection col refers to another LinkedList that will be added into the LinkedList.

Example: Java LinkedList addAll(Collection col) Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Add another new element to the new LinkedList using add( ) method.
  • Print the new LinkedList with the previous LinkedList using addAll(Collection col) method.

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(5);
        l2.add(6);
        // Prints the new LinkedList elements
        System.out.println("The elements of LinkedList 2 are: "+l2);
        // Adding both LinkedList l1 and l2 in a single LinkedList using addAll(Collection col) method
        l1.addAll(l2);
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList 1 are: "+l1);
    }
}
Output:

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

addAll(int index, Collection col)

This java.util.LinkedList.addAll(int index, Collection col) method adds/insert all the element of a collection to the LinkedList starting from the specified index position of the LinkedList.

It returns True if it successfully adds all the elements of collection to the LinkedList.

NullPointerException will come if the collection that you want to add does not have any element in it.

Syntax:

LinkedListName.addAll(int index, Collection col)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • Collection col refers to another LinkedList that will be added into the LinkedList.
  • int index refers to the LinkedList index position from where new collection elements will be added.

Example: Java LinkedList addAll(int index, Collection col) Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Add another new element to the new LinkedList using add( ) method.
  • Display the LinkedList elements.
  • Print the new LinkedList with the previous LinkedList using addAll(int index, Collection col) method to a specified position.

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(5);
        l2.add(6);
        // Prints the new LinkedList elements
        System.out.println("The elements of LinkedList 2 are: "+l2);
        // Adding both LinkedList l1 and l2 in a single LinkedList using addAll(int index, Collection col) method
        l1.addAll(2,l2);
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList 1 are: "+l1);
    }
}
Output:

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

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: