Concatenate multiple lists in Java
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:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Generic method to concatenate multiple lists in Java public static<T> List<T> concatenate(List<T>... lists) { List<T> result = new ArrayList<>(); for (List<T> l: lists) { result.addAll(l); } return result; } |
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.
|
1 2 3 4 5 6 7 |
// Generic method to concatenate multiple lists in Java public static<T> List<T> concatenate(List<T>... lists) { return Stream.of(lists) .flatMap(x -> x.stream()) .collect(Collectors.toList()); } |
We can also accumulate all elements using forEach(), as shown below:
|
1 2 3 4 5 6 7 8 |
// Generic method to concatenate multiple lists in Java public static<T> List<T> concatenate(List<T>... lists) { List<T> result = new ArrayList<>(); Stream.of(lists).forEach(result::addAll); return result; } |
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:
|
1 2 3 4 5 6 7 8 9 10 |
// Generic method to concatenate multiple lists in Java public static<T> List<T> concatenate(List<T>... lists) { Stream<T> stream = Stream.of(); for (List<T> l: lists) { stream = Stream.concat(stream, l.stream()); } return stream.collect(Collectors.toList()); } |
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:
|
1 2 3 4 |
// Generic method to concatenate multiple lists in Java public static<T> List<T> concatenate(List<T>... lists) { return Lists.newArrayList(Iterables.concat(lists)); } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Generic method to concatenate multiple lists in Java public static<T> List<T> concatenate(List<T>... lists) { List<T> result = Lists.newArrayList(); for (List<T> l: lists) { Iterables.addAll(result, l); } return result; } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Generic method to concatenate multiple lists in Java public static<T> List<T> concatenate(List<T>... lists) { List<T> result = new ArrayList<>(); for (List<T> l: lists) { result = ListUtils.union(result, l); } return result; } |
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:
|
1 2 3 4 5 6 7 8 9 10 |
// Generic method to concatenate multiple lists in Java public static<T> List<T> concatenate(List<T>... lists) { List<T> result = new ArrayList<>(); Iterable<T> iterable = IterableUtils.chainedIterable(lists); iterable.forEach(result::add); return result; } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 |
// Method to concatenate multiple lists in Java public static List<String> concatenate(List<String>... lists) { List<String> result = new ArrayList<>(); for (List<String> l: lists) { Collections.addAll(result, l.toArray(new String[0])); } return result; } |
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.
|
1 2 3 4 5 6 7 8 9 |
// Generic method to concatenate multiple lists in Java public static<T> List<T> concatenate(List<T>... lists) { return new ArrayList<T>() {{ for (List<T> l: lists) { addAll(l); } }}; } |
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.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)