Java LinkedList addAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, addAll(c) takes every element of another collection and adds it to the end of the LinkedList, in the order the other collection returns them. The second form, addAll(index, c), inserts the group at a position.

It is the quick way to merge two lists or to load a list from an ArrayList, a set, or a fixed list made with Arrays.asList. Only the references are copied, so the source collection is not changed.

An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, and LinkedList implements it.

Syntax

Pass any collection of a compatible element type.

Java
LinkedList<String> a = new LinkedList<>();
a.add("A");
boolean changed = a.addAll(Arrays.asList("B", "C"));  // true, [A, B, C]
a.addAll(1, Arrays.asList("X", "Y"));                 // [A, X, Y, B, C]
CallWhat it does
list.addAll(c)Appends all elements of c to the end.
list.addAll(index, c)Inserts all elements of c starting at index.
Collections.addAll(list, a, b, c)Adds individual values without building a collection first.
new LinkedList<>(c)Creates a new list that already contains the elements of c.

The result is true if the list changed, so adding an empty collection returns false.

Library declarations
Java
// java.util.LinkedList
public boolean addAll(Collection<? extends E> c)
public boolean addAll(int index, Collection<? extends E> c)

Parameters

ParameterDescription
c (Collection<? extends E>)The collection whose elements are added. It must not be null.
index (int)Position of the first inserted element, from 0 to size(). Only used by the two-argument form.

Return Value

Return ValueDescription
booleantrue if the list changed, false if c was empty.

Compare with Similar Methods

Related ways to combine data:

MethodHow it differs
add(e)Adds one element at a time.
retainAll(c)Keeps only the elements that also appear in another collection.
removeAll(c)Removes every element that appears in another collection.
Collections.addAll()Adds a handful of loose values to any collection.

Java Examples

Example 1 — Merge Two Lists into One

Start with one list of weekday names and add a second list to the end of it.

Java
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class LinkedListAddAllMerge {
    public static void main(String[] args) {
        LinkedList<String> days = new LinkedList<>();
        days.add("Mon");
        days.add("Tue");

        List<String> more = Arrays.asList("Wed", "Thu", "Fri");
        boolean changed = days.addAll(more);

        System.out.println("days: " + days);
        System.out.println("changed: " + changed);
        System.out.println("source untouched: " + more);
    }
}

How It Works

  1. A starting list. days holds Mon and Tue.
  2. Whole group appended. Wed, Thu and Fri are added after Tue, in the same order as in more.
  3. The list changed. At least one element was added, so addAll returns true.
  4. The source stays the same. more is only read, never modified.

Example 2 — Insert Several Elements at an Index

Insert a group of numbers into the middle of a list, then try an empty collection.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListAddAllIndex {
    public static void main(String[] args) {
        LinkedList<Integer> nums = new LinkedList<>(Arrays.asList(1, 2, 6, 7));

        nums.addAll(2, Arrays.asList(3, 4, 5));
        System.out.println("inserted: " + nums);

        boolean changed = nums.addAll(0, new ArrayList<Integer>());
        System.out.println("empty add changed: " + changed);

        nums.addAll(nums);
        System.out.println("doubled: " + nums);
    }
}

How It Works

  1. Four numbers to begin with. The constructor copies 1, 2, 6 and 7 into a new LinkedList.
  2. Group goes in at index 2. 3, 4 and 5 are placed between 2 and 6, keeping their own order.
  3. An empty collection adds nothing. The list is unchanged, so addAll reports false.
  4. Adding a list to itself. Java copies the elements first, so the whole sequence 1 to 7 appears twice.

Example 3 — Add Elements from a Set and Handle null

Copy the elements of a HashSet into a list, then pass a null collection to see the exception.

Java
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

public class LinkedListAddAllNull {
    public static void main(String[] args) {
        Set<String> colors = new HashSet<>();
        colors.add("red");

        LinkedList<String> list = new LinkedList<>();
        list.add("blue");
        list.addAll(colors);
        System.out.println("list: " + list);

        try {
            list.addAll(null);
        } catch (NullPointerException e) {
            System.out.println("addAll(null) -> NullPointerException");
        }
        System.out.println("size: " + list.size());
    }
}

How It Works

  1. Any collection works. The source can be a Set; addAll only needs its elements. Here the set has one color so the order is certain.
  2. Appended after blue. The list shows blue first, then red.
  3. A null collection is a mistake. Java cannot read elements from nothing, so it throws NullPointerException.
  4. The list is safe. The failed call changes nothing, and size() is still 2.

Frequently Asked Questions

Call first.addAll(second). This appends every element of the second list to the end of the first list.
It returns true if the list changed and false if the collection you passed was empty.
Yes. Use addAll(index, collection). The elements go in starting at that index, and later elements move back.
No. It copies the references, so both lists point to the same objects. The source collection itself is not changed.
It throws a NullPointerException. Pass an empty collection if you want to add nothing.

Next: addFirst()

Insert an element at the front (head) of the list.

addFirst() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful