Concatenate multiple arrays in Java
This post will discuss how to concatenate multiple arrays in Java into a single new array. The new array should maintain the original order of elements in individual arrays.
1. Using Java 8
We can use Stream in Java 8 and above to concatenate multiple arrays. There are various ways to do that:
⮚ Stream.of() method
We can obtain a stream consisting of all elements from every array using the static factory method Stream.of(). Once we have the stream, we can flatten it using the flatMap() method and then convert it back to an array using the toArray() method.
|
1 2 3 4 5 6 7 |
// Method to concatenate multiple arrays in Java 8 and above public static String[] concatenate(String[] ...arrays) { return Stream.of(arrays) .flatMap(Stream::of) // or, use `Arrays::stream` .toArray(String[]::new); } |
⮚ Stream.concat() method
The stream has a concat() method that takes two streams as input and creates a lazily concatenated stream out of them. We can use it to concatenate multiple arrays, as shown below:
|
1 2 3 4 5 6 7 8 9 10 |
// Method to concatenate multiple arrays in Java 8 and above public static String[] concatenate(String[] ...arrays) { Stream<String> stream = Stream.of(); for (String[] s: arrays) { stream = Stream.concat(stream, Arrays.stream(s)); } return stream.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 all arrays by using Arrays.copyOf(). 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 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Method to concatenate multiple arrays in Java public static String[] concatenate(String[]... arrays) { int finalLength = 0; for (String[] array: arrays) { finalLength += array.length; } String[] dest = null; int destPos = 0; for (String[] array: arrays) { if (dest == null) { dest = Arrays.copyOf(array, finalLength); destPos = array.length; } else { System.arraycopy(array, 0, dest, destPos, array.length); destPos += array.length; } } return dest; } |
Here’s a version that works with generics:
|
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 |
// Generic method to concatenate multiple arrays of the same type in Java public static <T> T[] concatenate(T[]... arrays) { int finalLength = 0; for (T[] array: arrays) { finalLength += array.length; } T[] dest = null; int destPos = 0; for (T[] array: arrays) { if (dest == null) { dest = Arrays.copyOf(array, finalLength); destPos = array.length; } else { System.arraycopy(array, 0, dest, destPos, array.length); destPos += array.length; } } return dest; } |
3. Using List
We can also use a list to concatenate multiple arrays in Java, as shown below. This approach is not recommended since it involves the creation of an intermediary list object.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Method to concatenate multiple arrays in Java 8 and above public static String[] concatenate(String[]... arrays) { List<String> result = new ArrayList<>(); Stream.of(arrays) .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 10 11 |
// Method to concatenate multiple arrays in Java public static String[] concatenate(String[]... arrays) { List<String> list = new ArrayList<>(); for (String[] array: arrays) { Collections.addAll(list, array); } return list.toArray(new String[0]); } |
That’s all about concatenating multiple 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 :)