Java join two arrays example shows how to join two arrays in Java. The example also shows various ways to join two arrays using the arraycopy, Apache Commons, Collections, and Java 8 Streams.
How to join two arrays in Java?
There are various ways using which you can join two arrays in Java.
1) Join two arrays using the arraycopy method of the System class
You can use the arraycopy
method of System class to copy elements to a third array one by one.
1 |
public static void arraycopy(Object source, int srcStartPosition, Object destination, int destStartPosition, int length) |
This method copies elements of the source array starting from srcStartPosition to specified destStartPosition of the destination array. The length parameter denotes the number of elements to be copied from source to destination.
This method may throw ArrayIndexOutOfBoundsException
, NullPointerException
or ArrayStoreException
.
Please see the below-given example that shows how to copy an array using this method.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.javacodeexamples.stringexamples; import java.util.Arrays; public class JoinArraysExample { public static void main(String[] args) { int[] iArray1 = {1, 2, 3}; int[] iArray2 = {4, 5, 6, 7}; /* * Define third int array which will hold * the combined elements of both arrays after * joining. * * The total number of elements required in third array * will be array 1 length + array 2 length */ int[] iJoinedArray = new int[ iArray1.length + iArray2.length ]; /* * Copy first array to third array starting * at 0 index of the third array */ System.arraycopy(iArray1, 0, iJoinedArray, 0, iArray1.length); /* * Now copy second array to third array starting from iArray.length * index of third array */ System.arraycopy(iArray2, 0, iJoinedArray, iArray1.length, iArray2.length); System.out.println("Joined array contains: "); System.out.println( Arrays.toString(iJoinedArray) ); } } |
Output
1 2 |
Joined array contains: [1, 2, 3, 4, 5, 6, 7] |
2) Join two arrays using Apache commons library
If you are using the Apache Commons library, you can use the addAll
method of ArrayUtils class.
1 |
public static <T> T[] addAll(T[] array1, T… array2) |
This method adds all elements of the specified arrays to a new array.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javacodeexamples.stringexamples; import java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; public class JoinArraysExample { public static void main(String[] args) { int[] iArray1 = {1, 2, 3}; int[] iArray2 = {4, 5, 6, 7}; /* * Use addAll method of ArrayUtils class to join * two arrays. */ int[] iJoinedArray = ArrayUtils.addAll(iArray1, iArray2); System.out.println("Joined array contains: "); System.out.println( Arrays.toString(iJoinedArray) ); } } |
Output
1 2 |
Joined array contains: [1, 2, 3, 4, 5, 6, 7] |
3) Join two arrays using the Collections class
Though not efficient, you can use the Collections class to join two arrays using the addAll
method.
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 26 27 28 29 30 31 32 |
package com.javacodeexamples.stringexamples; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class JoinArraysExample { public static void main(String[] args) { //source String arrays String[] strArray1 = {"One", "Two", "Three"}; String[] strArray2 = {"Four", "Five"}; //Create new List List<String> list = new ArrayList<String>(); //add all elements of array 1 to the list Collections.addAll(list, strArray1); //add all elements of array 2 to the list Collections.addAll(list, strArray2); //convert collection back to an array String[] strJoinedArray = list.toArray(new String[list.size()]); System.out.println("Join String array"); System.out.println( Arrays.toString(strJoinedArray) ); } } |
Output
1 2 |
Join String array [One, Two, Three, Four, Five] |
4) Join two arrays using Java 8 streams
If you are using Java version 8 or above, you can use streams to join two arrays in a single line as given below.
1 2 3 4 5 6 7 8 9 |
//source String arrays String[] strArray1 = {"One", "Two", "Three"}; String[] strArray2 = {"Four", "Five"}; String[] strJoinedArray = Stream .concat( Arrays.stream( strArray1 ), Arrays.stream( strArray2 ) ) .toArray(String[]::new); System.out.println( Arrays.toString(strJoinedArray) ); |
Output
1 |
[One, Two, Three, Four, Five] |
You can also use flatMap
and toArray
of Stream to join arrays as given below.
1 2 3 4 5 6 7 8 9 |
//source String arrays String[] strArray1 = {"One", "Two", "Three"}; String[] strArray2 = {"Four", "Five"}; String[] strJoinedArray = Stream.of( strArray1, strArray2 ) .flatMap( Stream::of ) .toArray( String[]::new); System.out.println( Arrays.toString(strJoinedArray) ); |
Output
1 |
[One, Two, Three, Four, Five] |
What is the best way to join two arrays in Java?
Using the Stream and Collections approach (3 and 4) will be the slowest way in terms of performance. Using Apache Commons (approach 2) is the simplest way to join two arrays while using the arraycopy
method of Systems class (approach 1) is the fastest and most efficient way to join two arrays in Java.
This example is a part of the Java Array tutorial with examples.
Please let me know your views in the comments section below.