Join two arrays in Java
This post will discuss how to join two primitives arrays in Java. The solution should contain all elements of the first array, followed by all elements the second array.
1. Using Java 8
We know that Java 8 has IntStream to deal with primitive ints. The idea is to get IntStream of elements present in both arrays and concatenate them using IntStream.concat() method. Finally, convert IntStream back to primitive int[] using toArray().
|
1 2 3 4 5 6 |
// Method to join two arrays in Java 8 and above public static int[] join(int[] a, int[] b) { return IntStream.concat(Arrays.stream(a), Arrays.stream(b)) .toArray(); } |
We can also use IntStream.of() method in place of Arrays.stream() for getting the IntStream from primitive int array.
|
1 2 3 4 5 6 |
// Method to join two arrays in Java 8 and above public static int[] join(int[] a, int[] b) { return IntStream.concat(IntStream.of(a), IntStream.of(b)) .toArray(); } |
For a primitive array of double and long type, we can use DoubleStream and LongStream, respectively. For example,
|
1 2 3 4 5 6 |
// Method to join two arrays in Java 8 and above public static double[] join(double[] a, double[] b) { return DoubleStream.concat(DoubleStream.of(a), DoubleStream.of(b)) .toArray(); } |
2. Using System.arraycopy() method
System.arraycopy() provides a convenient and very efficient way to copy arrays in Java. We can make use of this method to copy elements of the given arrays into a newly allocated empty array.
|
1 2 3 4 5 6 7 8 9 10 |
// Method to join two arrays in Java public static int[] join(int[] a, int[] b) { int[] c = new int[a.length + b.length]; System.arraycopy(a, 0, c, 0, a.length); System.arraycopy(b, 0, c, a.length, b.length); return c; } |
We can also use Arrays.copyOf() along with System.arraycopy(), as shown below:
|
1 2 3 4 5 6 7 8 |
// Method to join two arrays in Java public static int[] join(int[] a, int[] b) { int[] result = Arrays.copyOf(a, a.length + b.length); System.arraycopy(b, 0, result, a.length, b.length); return result; } |
3. Using Guava Library
Guava provides several utility classes pertaining to primitives, like Ints for int, Floats for float, Doubles for double, Longs for long, Booleans for boolean, and so on. Each utility class has a concat() method that can combine specified primitive arrays into a single array.
|
1 2 3 4 |
// Method to join two arrays in Java public static int[] join(int[] a, int[] b) { return Ints.concat(a, b); } |
4. Using Apache Commons Lang
Apache Commons Lang also provides static utility methods pertaining to int primitives that are not already found in either Integer or Arrays. One such method is ArrayUtils.addAll(), which can join two arrays. This method is overloaded for all primitives types and object arrays.
|
1 2 3 4 |
// Method to join two arrays in Java public static int[] join(int[] a, int[] b) { return ArrayUtils.addAll(a, b); } |
Please note that all the above-mentioned methods will throw a NullPointerException if any specified array is null. We can easily add the following snippet at the starting of each method to handle nulls.
|
1 2 3 4 5 6 7 |
if (a == null) { return b; } if (b == null) { return a; } |
That’s all about joining 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 :)