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:

Download  Run Code

 
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:

Download  Run Code

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.

Download  Run Code

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.

Download Code

 
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.

Download  Run Code

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.