Join Multiple Lists in Java
This post will discuss how to join multiple lists in Java. The solution creates a new list containing all elements of the provided lists, in iteration order.
1. Using Stream.flatMap() method
You can easily join multiple lists with the help of the flatMap() method provided by Java 8 Stream API. The idea is to create a stream of lists, and flatten each list using the flatMap() method, and finally collect the elements in a new list. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { List<String> first = Arrays.asList("C", "C++", "C#"); List<String> second = Arrays.asList("Java", "Kotlin"); List<String> third = Arrays.asList("Ruby", "JavaScript"); List<String> languages = Stream.of(first, second, third) .flatMap(List::stream) .collect(Collectors.toList()); System.out.println(languages); } } |
Output:
[C, C++, C#, Java, Kotlin, Ruby, JavaScript]
Since the Stream.of() method uses varargs, you can create a generic utility method to concat an arbitrary number of lists.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static <T> List<T> concat(List<T>... lists) { return Stream.of(lists) .flatMap(List::stream) .collect(Collectors.toList()); } public static void main(String[] args) { List<String> first = Arrays.asList("C", "C++", "C#"); List<String> second = Arrays.asList("Java", "Kotlin"); List<String> third = Arrays.asList("Ruby", "JavaScript"); List<String> languages = concat(first, second, third); System.out.println(languages); } } |
Output:
[C, C++, C#, Java, Kotlin, Ruby, JavaScript]
Note that Stream.of() is just a wrapper over Arrays.stream(), and both methods can be used interchangeably except for the primitive arrays:
|
1 2 3 4 5 |
public static <T> List<T> concat(List<T>... lists) { return Arrays.stream(lists) .flatMap(List::stream) .collect(Collectors.toList()); } |
2. Using List.addAll() method
A straightforward solution is to create a new list, and add all the elements in each list to the end of this list using the addAll() method. This can be done using a simple for loop, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static <T> List<T> concat(List<T>... lists) { List<T> newList = new ArrayList<>(); for (List<T> list : lists) { newList.addAll(list); } return newList; } public static void main(String[] args) { List<String> first = Arrays.asList("C", "C++", "C#"); List<String> second = Arrays.asList("Java", "Kotlin"); List<String> third = Arrays.asList("Ruby", "JavaScript"); List<String> languages = concat(first, second, third); System.out.println(languages); } } |
Output:
[C, C++, C#, Java, Kotlin, Ruby, JavaScript]
Here’s an equivalent version using the Stream API. It takes advantage of the forEach() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static <T> List<T> concat(List<T>... lists) { List<T> newList = new ArrayList<>(); Arrays.stream(lists).forEach(newList::addAll); return newList; } public static void main(String[] args) { List<String> first = Arrays.asList("C", "C++", "C#"); List<String> second = Arrays.asList("Java", "Kotlin"); List<String> third = Arrays.asList("Ruby", "JavaScript"); List<String> languages = concat(first, second, third); System.out.println(languages); } } |
Output:
[C, C++, C#, Java, Kotlin, Ruby, JavaScript]
3. Using Stream.concat() method
If you want to join only two lists, you can use the Stream.concat() method. The idea is to get a stream of elements from both lists and pass each stream to the Stream.concat() method. It creates a lazily concatenated stream whose elements are all the elements of the first stream, followed by all the elements of the second stream.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { List<String> first = Arrays.asList("C", "C++"); List<String> second = Arrays.asList("Java", "Kotlin"); List<String> languages = Stream.concat(first.stream(), second.stream()) .collect(Collectors.toList()); System.out.println(languages); } } |
Output:
[C, C++, Java, Kotlin]
4. Using Apache Commons Collections
Apache Commons Collections ListUtils.union() method returns a new list containing the concatenation of the provided lists. You can use it to join two lists, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.apache.commons.collections4.ListUtils; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> first = Arrays.asList("C", "C++"); List<String> second = Arrays.asList("Java", "Kotlin"); List<String> languages = ListUtils.union(first, second); System.out.println(languages); } } |
Output:
[C, C++, Java, Kotlin]
That’s all about joining 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 :)