Concatenate object arrays in Java
This post will discuss how to concatenate object arrays into a single new object array in Java.
There are several ways to concatenate object arrays in Java, where the new array maintains the original order of elements in individual arrays, and all elements of the first array precede all elements of the second array. Here are some of the common methods:
1. Using Stream API
A simple way to concatenate object arrays in Java using the Stream.of(T…) method from the Java 8 Stream API. This method takes one or more arrays and creates a stream that contains all the elements of the arrays. Then, we can use the Stream.toArray() method to collect the stream elements into a new array. Alternatively, we can use the Stream.of(T…) method to create streams from the arrays, and then use the Stream.concat() method to merge the streams. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Arrays; import java.util.stream.Stream; class Main { public static String[] concatenate1(String[] a, String[] b) { return Stream.of(a, b) .flatMap(Stream::of) // or Arrays::stream .toArray(String[]::new); } public static String[] concatenate2(String[] a, String[] b) { return Stream.concat(Stream.of(a), Stream.of(b)) // or Arrays::stream .toArray(String[]::new); } public static void main(String[] args) { String[] s1 = { "a", "b", "c" }; String[] s2 = { "d", "e" }; String[] arr = concatenate1(s1, s2); System.out.println(Arrays.toString(arr)); // [a, b, c, d, e] } } |
This method works for any type of object arrays, as long as they are compatible with the generic type parameter T of the Stream interface. For primitive arrays, we can use the corresponding stream classes, such as IntStream, LongStream, or DoubleStream, and use their concat() methods.
2. Using System.arraycopy() method
Another option is to use the System.arraycopy() method from the Java System class. This method copies a specified number of elements from a source array to a destination array, starting from specified positions. We can use this method to concatenate the elements of two arrays into a new array. We can also make use of the Arrays.copyOf() method to create a new array from elements of one array. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.Arrays; class Main { // A method to concatenate string arrays using System.arraycopy() public static String[] concatenate1(String[] a, String[] b) { // create an empty object array with enough space // to allocate elements of both arrays String[] arr = new String[a.length + b.length]; // copy first array to object array System.arraycopy(a, 0, arr, 0, a.length); // copy the second array to the object array System.arraycopy(b, 0, arr, a.length, b.length); return arr; } // Another method to concatenate string arrays using Arrays.copyOf() and System.arraycopy() public static String[] concatenate2(String[] a, String[] b) { // copy first array to object array String[] arr = Arrays.copyOf(a, a.length + b.length); // copy the second array to the object array System.arraycopy(b, 0, arr, a.length, b.length); return arr; } public static void main(String[] args) { String[] s1 = { "a", "b", "c" }; String[] s2 = { "d", "e" }; String[] arr = concatenate1(s1, s2); System.out.println(Arrays.toString(arr)); // [a, b, c, d, e] } } |
3. Using a List
Another approach is to use a Java collection to concatenate two arrays by creating an intermediary list from elements of both arrays and then converting the list back to the object array. To illustrate, consider the following methods that uses Java 8 Stream API and Collections.addAll() method to create an intermediary list object. This approach should be best avoided and System.arraycopy() should be used instead to ensure better performance.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Stream; class Main { // A method to concatenate string arrays using Stream API public static String[] concatenate1(String[] a, String[] b) { List<String> list = new ArrayList<>(); Stream.of(a, b) .flatMap(Arrays::stream) .forEach(list::add); return list.toArray(String[]::new); } // Another method to concatenate string arrays using Collections.addAll() method public static String[] concatenate2(String[] a, String[] b) { List<String> list = new ArrayList<>(); Collections.addAll(list, a); Collections.addAll(list, b); return list.toArray(String[]::new); } public static void main(String[] args) { String[] s1 = { "a", "b", "c" }; String[] s2 = { "d", "e" }; String[] arr = concatenate1(s1, s2); System.out.println(Arrays.toString(arr)); // [a, b, c, d, e] } } |
4. Using Guava Library
Guava library provides ObjectArrays class that has the concat(T[], T[], Class<T>) method, which takes two arrays of the same type and a class object representing the type of the arrays, and returns a new array that contains all the elements of both arrays in order. This method works for any type of object arrays, as long as they are compatible with the generic type parameter T of the ObjectArrays class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import com.google.common.collect.ObjectArrays; import java.util.Arrays; class Main { public static String[] concatenate(String[] a, String[] b) { return ObjectArrays.concat(a, b, String.class); } public static void main(String[] args) { String[] s1 = { "a", "b", "c" }; String[] s2 = { "d", "e" }; String[] arr = concatenate(s1, s2); System.out.println(Arrays.toString(arr)); // [a, b, c, d, e] } } |
To join arrays of primitive types, we can use the concat() methods of the respective primitive classes. This is a flexible and powerful way to concatenate arrays, but it also requires adding an external dependency to our project.
5. Using Apache Commons Lang Library
Another convenient and easy way to concatenate arrays is to use the ArrayUtils.addAll(T[], T…) method from the Apache Commons Lang library. This method takes two arrays of the same type and returns a new array that contains all the elements of both arrays in order. This method works for both primitive and object arrays, and has different variants for different types. This method can be used when the project already has Apache Commons Lang dependency.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; class Main { // Generic method to concatenate arrays of the same type in Java public static <T> T[] concatenate(T[] a, T[] b) { return ArrayUtils.addAll(a, b); } public static void main(String[] args) { String[] s1 = { "a", "b", "c" }; String[] s2 = { "d", "e" }; String[] arr = concatenate(s1, s2); System.out.println(Arrays.toString(arr)); // [a, b, c, d, e] } } |
Finally, we can also iterate over the elements of the arrays and create a new array containing all the elements. This is a simple and straightforward approach, but it requires writing some boilerplate code and handling the array index manually. The implementation of this approach is left as an exercise for the readers. That’s all about concatenating object arrays into a single new object array 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 :)