LinkedList addAll() Method in Java (original) (raw)

Last Updated : 18 Dec, 2024

In Java, the addAll() method of the LinkedList class is used to add all the elements of one collection to another.This method takes a Collection as an argument and adds all its elements to the end of the list.

**Example: Here, we use the **addAll() method **to add all the elements of one collection to another.

Java `

// Java Programm to demonstrate the // working of addAll() in LinkedList import java.util.LinkedList;

public class Geeks { public static void main(String args[]) {

    // Create an empty LinkedList
    LinkedList<String> l1 = new LinkedList<>();

    // Use add() method to add 
    // elements in the list
    l1.add("Geeks");
    l1.add("for");
    l1.add("Geeks");

    // Created another list
    LinkedList<String> l2 = new LinkedList<>();
    l2.add("10");
    l2.add("20");

    System.out.println("" + l1);

    // Appending the collection 
    // to the list
    l1.addAll(l2);

    System.out.println("" + l1);
}

}

`

Output

[Geeks, for, Geeks] [Geeks, for, Geeks, 10, 20]

**Now there are two versions of LinkedList addAll() method i.e. one with the specified index and one without any specified index.

1. **addAll(Collection)

The **addAll() method takes a Collection as an argument and adds all elements from the specified collection to the end of the current LinkedList.

**Syntax:

public boolean addAll(Collection)

**Parameters:

**Return Type: This method returns true, if at least one action of append is performed.

Example: Here, we use addAll() method_to add all the elements of one collection to another at the end of the list_.**

Java `

// Add all the elements of one collection to another import java.util.LinkedList;

public class Geeks { public static void main(String args[]) {

    // Create an empty list
    LinkedList<Integer> l1 = new LinkedList<>();

    // Use add() method to add 
    // elements in the list
    l1.add(100);
    l1.add(200);
    l1.add(300);

    // Create another list
    LinkedList<Integer> l2 = new LinkedList<>();
    l2.add(400);
    l2.add(500);
    l2.add(600);

    System.out.println("" + l1);

    // Appending the collection to the list
    l1.addAll(l2);

    System.out.println("" + l1);
}

}

`

Output

[100, 200, 300] [100, 200, 300, 400, 500, 600]

**2. addAll(index , Collection)

The addAll(index , Collection) method takes an index and a Collection as arguments and adds all elements from the specified collection starting at the given index. The subsequent elements are shifted to the right.

**Syntax:

public boolean.addAll(index , Collection);

**Parameters: This function accepts two parameters an index and Collection. The elements of the collection are inserted at the specified index in the list.

**Example: Here, we use the **addAll() method **to add all the elements from a collection at the specified index.

Java `

// Add all the elements from a // collection at the specified index import java.util.ArrayList; import java.util.LinkedList;

class Geeks { public static void main(String[] args) {

    // Create an empty list
    LinkedList<Integer> l1 = new LinkedList<>();
  
    // Use add() to insert elements 
    // in the list
    l1.add(100);
    l1.add(200);
    System.out.println("" + l1);

    // Create another collection (ArrayList)
    ArrayList<Integer> l2 = new ArrayList<>();

    l2.add(300);
    l2.add(400);

    // use addAll() to add all 
    // elements from l2 at index 1
    l1.addAll(1, l2);

    System.out.println("" + l1);
}

}

`

Output

[100, 200] [100, 300, 400, 200]