Java ArrayList addAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, addAll() adds every element of another collection to an ArrayList in one call. addAll(c) appends them at the end, in the order that collection's iterator returns them. addAll(index, c) inserts them as a block starting at index, shifting the existing elements to the right.

It returns true if the list changed, which for an ArrayList means the collection was not empty. The source collection is not modified; only references to its elements are copied into the list.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

Pass any Collection, optionally with a start index.

Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b"));
List<String> more = Arrays.asList("c", "d");
list.addAll(more);          // [a, b, c, d]
list.addAll(0, more);       // [c, d, a, b, c, d]
CallWhat it does
list.addAll(c)Appends all elements of c.
list.addAll(i, c)Inserts all elements of c starting at index i.
new ArrayList<>(c)Creates a new list that already holds the elements of c.
Collections.addAll(list, "x", "y")Adds loose values (varargs) instead of a collection.

ArrayList.addAll copies the collection into an array first and makes room for the whole batch at once, so it is usually faster than calling add() in a loop.

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

// Declared in Collection<E> and List<E>

Parameters

ParameterDescription
c (Collection<? extends E>)The elements to add. Any collection whose elements fit the list type works, such as a List<Integer> into a List<Number>. Must not be null.
index (int)Only in addAll(int, Collection). Where the first new element goes, from 0 to size().

Return Value

Return ValueDescription
booleantrue if the list changed (c had at least one element); false if c was empty.

Compare with Similar Methods

Related ways to combine or filter collections:

MethodHow it differs
add(e)Adds a single element.
removeAll(c)The opposite direction: removes every element that is in c.
retainAll(c)Keeps only the elements that are in c.
Collections.addAll(list, ...)Adds individual values without building a collection first.

Java Examples

Example 1 — Merge Two Lists

Append a list of newcomers to a team and check both lists afterwards.

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

public class ArrayListAddAllDemo {
    public static void main(String[] args) {
        List<String> team = new ArrayList<>(Arrays.asList("Ana", "Ben"));
        List<String> newcomers = Arrays.asList("Cleo", "Dev");

        boolean changed = team.addAll(newcomers);
        System.out.println(changed + " " + team);
        System.out.println("newcomers untouched: " + newcomers);
    }
}

How It Works

  1. Two inputs. team is a growable ArrayList; newcomers is a fixed-size list from Arrays.asList, used here only as the source of elements.
  2. One call, two adds. addAll appends Cleo and Dev after Ben, keeping their order.
  3. true means changed. The collection was not empty, so the team grew and the method returned true.
  4. Input left alone. addAll only reads newcomers, so it still prints [Cleo, Dev].

Example 2 — Insert a Block in the Middle

Fill a gap in a number sequence, then put two numbers in front.

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

public class ArrayListAddAllAtIndex {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 6));
        nums.addAll(2, Arrays.asList(3, 4, 5));
        System.out.println(nums);

        nums.addAll(0, Arrays.asList(-1, 0));
        System.out.println(nums + " size=" + nums.size());
    }
}

How It Works

  1. The gap. The list [1, 2, 6] is missing 3, 4, and 5, which belong at index 2.
  2. Block insert. addAll(2, ...) places 3, 4, and 5 at indexes 2, 3, and 4, and 6 slides from index 2 to index 5.
  3. At the front. Index 0 puts -1 and 0 before everything else, and the two keep their own order.
  4. Count. 3 starting numbers + 3 + 2 inserted numbers gives a size of 8.

Example 3 — Wider Types, an Empty Collection, and null

Add an Integer list and a Double list to a List<Number>, then try an empty collection and null.

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

public class ArrayListAddAllEdgeCases {
    public static void main(String[] args) {
        List<Number> values = new ArrayList<>();
        List<Integer> ints = Arrays.asList(7, 8);
        List<Double> doubles = Arrays.asList(1.5);

        System.out.println(values.addAll(ints) + " " + values.addAll(doubles) + " " + values);
        System.out.println("empty: " + values.addAll(new ArrayList<Integer>()));

        try {
            values.addAll(null);
        } catch (NullPointerException e) {
            System.out.println("null collection: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Wider element type. A List<Number> accepts both a List<Integer> and a List<Double>, thanks to the Collection<? extends E> parameter.
  2. Left to right. Java evaluates the parts of the string in order, so both addAll calls finish before values is printed as [7, 8, 1.5].
  3. Nothing to add. An empty collection leaves the list unchanged, so addAll returns false.
  4. null is not empty. Passing null instead of a collection throws NullPointerException.

Frequently Asked Questions

true if the list changed, which happens whenever the collection you pass has at least one element. An empty collection gives false.
Yes. The elements are added in the order the other collection returns them, and they keep the same order inside the list.
Create a copy of the first with new ArrayList<>(first), then call addAll(second) on the copy. Both original lists stay unchanged.
Yes. list.addAll(0, other) inserts all of its elements before the current first element.
It throws NullPointerException. To add nothing, pass an empty collection instead; the method then returns false.

Next: addFirst()

Insert an element at the front (Java 21+).

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