How to Concatenate Two Arrays in Java

Learn to concatenate two primitive arrays or objects arrays to create a new array consisting of the items from both arrays. We will learn to merge array items using simple for-loop, stream API and other utility classes.

Note that no matter which technique we use for merging the arrays, it always creates a new array of combined length of both arrays and then copies the items from both arrays into the new array, one at a time, in a loop. So the main difference between the various ways, discussed below, is code readability and ease of use.

1. Concatenating Arrays with ‘for loop’

Directly using the for-loop to merge the arrays is the most efficient in terms of performance. It does not create unnecessary temporary variables and objects.

We create a new array of combined length of two given arrays and then start putting items from both arrays into the new array in a loop. Theoretically, this method should give the best performance.

int[] intArray1 = {1, 2, 3};
int[] intArray2 = {4, 5, 6};

result = new int[intArray1.length + intArray2.length];
int index = 0;

for (int item: intArray1) {
  result[index++] = item;
}
for (int item: intArray2) {
  result[index++] = item;
}

System.out.println(Arrays.toString(result)); //[1, 2, 3, 4, 5, 6]

2. Using System.arraycopy()

Using arraycopy() is equivalent to using the for-loop. Here we delegate the task of copying the array items into a new array to the native function arraycopy().

Performance-wise, arraycopy() method should perform on par with the for-loop method.

int[] intArray1 = {1, 2, 3};
int[] intArray2 = {4, 5, 6};

int[] result = new int[intArray1.length + intArray2.length];

System.arraycopy(intArray1, 0, result, 0, intArray1.length);
System.arraycopy(intArray2, 0, result, intArray1.length, intArray2.length);

System.out.println(Arrays.toString(result)); //[1, 2, 3, 4, 5, 6]

We can write a generic type method that can be used to merge the arrays holding the items of any object type.

static <T> T[] concatArrays(T[] array1, T[] array2) {
	T[] result = Arrays.copyOf(array1, array1.length + array2.length);
	System.arraycopy(array2, 0, result, array1.length, array2.length);
	return result;
}

//Invoking the method
String[] resultObj = concatArrays(strArray1, strArray2); 

System.out.println(Arrays.toString(resultObj)); //[1, 2, 3, 4, 5, 6]

3. Merging Arrrays with Stream.concat()

The stream.concat() method creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

To obtain the streams, we convert the arrays into streams and then concat() to merge the streams. Later, we can collect the stream items into an array to complete the merge process.

String[] strArray1 = {"1", "2", "3"};
String[] strArray2 = {"4", "5", "6"};

String[] both = Stream.concat(Arrays.stream(strArray1), 
								Arrays.stream(strArray2))
    						.toArray(String[]::new);

We can use this technique to concatenate more than two arrays in a single statement.

String[] mergedArray = Stream.concat(Arrays.stream(strArray1), 
								Arrays.stream(strArray2),
								Arrays.stream(strArray3),
								Arrays.stream(strArray4))
    						.toArray(String[]::new);

To merge the array of primitives, we can directly use the following classes:

  • IntStream
  • LongStream
  • DoubleStream

These classes provide the concat() methods specific to these primitives.

int[] intArray1 = {1, 2, 3};
int[] intArray2 = {4, 5, 6};

int[] result = IntStream.concat(Arrays.stream(intArray1), 
									Arrays.stream(intArray2))
								.toArray();

4. ArrayUtils from Apache Commons

We can use the 3rd part libraries that provide easy-to-use one-line methods for merging the arrays. They also provide the same level of performance as previous methods.

A similar API is ArrayUtils.addAll() that adds all the elements of the given arrays into a new array. Note that the returned array always returns a new array, even if we are merging two arrays and one of them is null.

String[] resultObj = ArrayUtils.addAll(strArray1, strArray2);

int[] result = ArrayUtils.addAll(intArray1, intArray2);

5. Guava APIs

Guava APIs provide the following classes that we can use to merge arrays of primitives as well as arrays of object types.

  • Ints.concat(): concatenates two int arrays.
  • Longs.concat(): concatenates two long arrays.
  • Doubles.concat(): concatenates two double arrays.
  • ObjectArrays.concat(): concatenates two object type arrays.
String[] resultObj = ObjectArrays.concat(strArray1, strArray2, String.class);

int[] result = Ints.concat(intArray1, intArray2);

6. Conclusion

In this tutorial, we learned to merge two arrays in Java. We learned to use the native Java APIs as well as the utility classes from the 3rd party libraries.

All the above methods will give almost the same level of performance for small arrays. For big arrays, it is recommended to do performance testing before finalizing the approach. However theoretically, using the for-loop and arraycopy() method should provide the best performance among all.

Happy Learning !!

Sourcecode on Github

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.