Concatenate two arrays in Java
This post will discuss concatenating two arrays in Java into a new array. The new array should maintain the original order of elements in individual arrays, and all elements in the first array should precede all elements of the second array.
1. Using Java 8 Stream
We can use Stream in Java 8 and above to concatenate two arrays. There are various ways to do that:
⮚ Stream.of() method
|
1 2 3 4 5 6 7 |
// Method to concatenate two arrays in Java 8 and above public static String[] concatenate(String[] first, String[] second) { return Stream.of(first, second) .flatMap(Stream::of) // or, use `Arrays::stream` .toArray(String[]::new); } |
⮚ Stream.concat() method
|
1 2 3 4 5 6 |
// Method to concatenate two arrays in Java 8 and above public static String[] concatenate(String[] first, String[] second) { return Stream.concat(Arrays.stream(first), Arrays.stream(second)) .toArray(String[]::new); } |
2. Using System.arraycopy() method
We start by allocating enough memory to the new array to accommodate all the elements present in both arrays. Then we use System.arraycopy() to copy given arrays into the new array, as shown below:
|
1 2 3 4 5 6 7 8 9 10 |
// Method to concatenate two arrays in Java public static String[] concatenate(String[] first, String[] second) { String[] arr = new String[first.length + second.length]; System.arraycopy(first, 0, arr, 0, first.length); System.arraycopy(second, 0, arr, first.length, second.length); return arr; } |
Here’s a version that works with generics:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Generic method to concatenate arrays of the same type in Java public static<T> T[] concatenate(T[] first, T[] second) { T[] ob = (T[]) Array.newInstance(first.getClass().getComponentType(), first.length + second.length); System.arraycopy(first, 0, ob, 0, first.length); System.arraycopy(second, 0, ob, first.length, second.length); return ob; } |
Here’s another generic version that uses Arrays.copyOf() along with System.arraycopy():
|
1 2 3 4 5 6 7 |
// Generic method to concatenate arrays of the same type in Java public static<T> T[] concatenate(T[] first, T[] second) { T[] result = Arrays.copyOf(first, first.length + second.length); System.arraycopy(second, 0, result, first.length, second.length); return result; } |
3. Using List
We can also use a list to concatenate two arrays, as shown below. This approach is not suggested over Java 8, and the System.arraycopy() method discussed earlier since it involves creating an intermediary list object.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Method to concatenate two arrays in Java 8 and above public static String[] concatenate(String[] first, String[] second) { List<String> result = new ArrayList<>(); Stream.of(first, second) .flatMap(Stream::of) .map(x -> (String)x) .forEach(result::add); return result.toArray(new String[0]); } |
For Java 7 and before, we can use Collections.addAll() method:
|
1 2 3 4 5 6 7 8 9 |
// Method to concatenate two arrays in Java public static String[] concatenate(String[] first, String[] second) { List<String> list = new ArrayList<>(); Collections.addAll(list, first); Collections.addAll(list, second); return list.toArray(new String[0]); } |
or
|
1 2 3 4 5 6 7 8 |
// Method to concatenate two arrays in Java public static String[] concatenate(String[] first, String[] second) { List<String> list = new ArrayList<>(Arrays.asList(first)); Collections.addAll(list, second); return list.toArray(new String[0]); } |
4. Using Guava Library
Guava library provides ObjectArrays class that has the concat() method, which returns a new array of the specified type containing concatenated contents of two arrays.
|
1 2 3 4 |
// Method to concatenate two arrays in Java public static String[] concatenate(String[] first, String[] second) { return ObjectArrays.concat(first, second, String.class); } |
That’s all about concatenating two arrays 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 :)