1. Overview
In Java, ArrayList is a commonly used List implementation. There are scenarios when we might need to add elements from multiple String arrays to an ArrayList.
In this quick tutorial, let’s explore how to accomplish this task efficiently.
2. Introduction to the Problem
Before diving into the code, let’s quickly understand the problem through an example.
Let’s say we have three String arrays:
final static String[] ARRAY1 = { "Java", "Kotlin", "Sql", "Javascript" };
final static String[] ARRAY2 = { "C", "C++", "C#", "Typescript" };
final static String[] ARRAY3 = { "Python", "Ruby", "Go", "Rust" };
Also, we have a method to produce a String List with two String elements:
List<String> initLanguageList() {
List<String> languageList = new ArrayList<>();
languageList.add("Languages");
languageList.add(":");
return languageList;
}
If we correctly add String elements from the three arrays in turn to the List that initLanguageList() produced, we expect to have a List carrying the following elements:
final static List<String> EXPECTED = List.of(
"Languages", ":",
"Java", "Kotlin", "Sql", "Javascript",
"C", "C++", "C#", "Typescript",
"Python", "Ruby", "Go", "Rust");
We can use List’s add() and addAll() methods to add one single String or a String Collection to a List
Next, we’ll address different ways to add elements from multiple arrays to a List.
3. Converting Arrays to Lists and Then Using the addAll() Method
We know the List.addAll() method can add multiple elements to a List. But it accepts a Collection parameter instead of an array. So, the first idea to solve the problem is to *convert arrays to Collections, such as Lists, and then use List.addAll() to add array elements to the List.* Moreover, to convert an array to a List, we can use the Arrays.asList() method:
List<String> languageList = initLanguageList();
for (String[] array : List.of(ARRAY1, ARRAY2, ARRAY3)) {
languageList.addAll(Arrays.asList(array));
}
assertEquals(EXPECTED, languageList);
In this example, we first wrap the three arrays in a List. Then, we pass through each array using a loop. In the loop, we convert each array to a List, and pass it to the addAll() method.
4. Using the Collections.addAll() Method
We’ve noted List.addAll() doesn’t accept an array as the parameter. So, we converted arrays to Lists to feed List.addAll(). However, Collections.addAll() supports adding multiple elements as Varargs to a Collection object:
public static <T> boolean addAll(Collection<? super T> c, T... elements)
As Java treats Varargs argument as an array, we can directly pass an array as a parameter to Collections.addAll():
List<String> languageList = initLanguageList();
for (String[] array : List.of(ARRAY1, ARRAY2, ARRAY3)) {
Collections.addAll(languageList, array);
}
assertEquals(EXPECTED, languageList);
As the test code shows, we replaced the previous List.addAll() call with Collections.addAll(), and we don’t need to convert arrays to Lists.
5. Using Stream‘s flatMap() Method
Java Stream API allows us to manipulate Collections fluently and conveniently. Next, let’s solve the problem using Stream API:
List<String> languageList = initLanguageList();
Stream.of(ARRAY1, ARRAY2, ARRAY3)
.flatMap(Arrays::stream)
.forEachOrdered(languageList::add);
assertEquals(EXPECTED, languageList);
As we can see, we first create a Stream object carrying the three String arrays. The flatMap() method is helpful in transforming and flattening collections of collections, including arrays of arrays, into a single stream. In this example, we use this method to flatten the steam of arrays into a single stream of String values.
Then, we pass a method reference (languageList::add) to Stream‘s forEachOrdered() method to add each array element to languageList.
If we run the test, it passes. The Stream.flatMap() approach leverages the power of Java streams to transform and flatten data structures, making our code concise and readable.
6. Conclusion
In this article, we’ve explored various ways to add elements from multiple String arrays to an ArrayList in Java. We can use the Arrays.asList() method or Collections.addAll() to solve the problem. Additionally, using Stream.flatMap() to add elements from multiple String arrays to an ArrayList is an efficient and elegant approach.
As always, the complete source code for the examples is available over on GitHub.