In this article, several methods to concatenate multiple lists in Java into a single list are discussed using plain Java, Guava library, and Apache Commons Collections.

1. Using List.addAll() method

List.addAll(Collection) method concatenates all elements of the specified collection at the end of the list. We can use it inside a for-each loop to concatenate multiple lists, as shown below:

2. Using Stream.of() method

We can also use streams in Java 8 and above to concatenate multiple lists by obtaining a stream consisting of all elements from every list using the static factory method Stream.of() and accumulating all elements into a new list using a Collector.

We can also accumulate all elements using forEach(), as shown below:

3. Using Stream.concat() method

The stream has a concat() method that takes two streams as input and creates a lazily concatenated stream out of them. We can use it to concatenate multiple lists, as shown below:

4. Using Guava’s Iterables Class

Guava’s Iterables class provides many static utility methods that operate on or return objects of type Iterable.

1. concat() can be used to combine several iterables into a single iterable, as shown below:

We can also use ImmutableList.copyOf() in place of Lists.newArrayList() to return an immutable list.

 
2. addAll(Iterable, Collection) adds all elements in the specified iterable to the collection. We can use it inside a for-each loop to concatenate multiple lists, as shown below:

5. Using Apache Commons Collections

Apache Commons Collections ListUtils.union() method takes two lists as an input and returns a new list containing the second list appended to the first list. We can use it to concatenate multiple lists, as shown below:

 
Apache Commons Collections also provides the IterableUtils class that contains several utility methods for Iterable instances. One such method is chainedIterable(), which can combine multiple Iterables into a single one, as shown below:

6. Using Collections.addAll() method

Collections class provides addAll(Collection, T[]) method that adds specified elements to the specified collection. This method is similar to List.addAll() but offers better performance.

7. Using Double Brace Initialization

We can also use Double Brace Initialization, which internally creates an anonymous inner class with an instance initializer in it.

 
We should best avoid this technique as it costs an extra class at each usage. It also holds hidden references to the enclosing instance and any captured objects. This may cause memory leaks or problems with serialization.

That’s all about concatenating multiple lists in Java.