How to Merge Two Arrays in Java
Merging two arrays in Java is similar to concatenate or combine two arrays in a single array object. We have to merge two arrays such that the array elements maintain their original order in the newly merged array. The elements of the first array precede the elements of the second array in the newly merged array. For example:
There are following ways to merge two arrays:
Java arraycopy() method
Java arraycopy() is the method of System class which belongs to java.lang package. It copies an array from the specified source array to the specified position of the destination array. The number of elements copied is equal to the length argument.
It throws NullPointerException if the source or destination array is null. It also throws ArrayIndexOutOfBoundsException if:
Example of arraycopy() method
In the following example, we have created two integer arrays firstArray and secondArray. In order to merge two arrays, we find its length and stored in fal and sal variable respectively. After that, we create a new integer array result which stores the sum of length of both arrays. Now, copy each elements of both arrays to the result array by using arraycopy() function.
[23, 45, 12, 78, 4, 90, 1, 77, 11, 45, 88, 32, 56, 3]
Let's see another example in which we have specified soure_array, destination, dest_position, source position, and length. We can merge array according to the specified positions and length.
source_array: 11 22 33 44 55 98 76 54 60 sourcePos: 2 dest_array: 66 77 88 99 22 67 21 90 80 70 destPos: 4 len: 3 Resultant array: 66 77 88 99 33 44 55 90 80 70
Without using arraycopy() method
Example of merging two arrays
In the following example, we have initialized two arrays firstArray and secondArray of integer type. Manually copy the each element of both arrays to mergedArray and convert that array into String by using toString() method of Array class.
[56, 78, 90, 32, 67, 12, 11, 14, 9, 5, 2, 23, 15]
Example of merging two arrays in Java
In the following example, we have initialized two arrays str1 and str2 of String type. After that we have created a list view of str1 by using the Arrays.asList() method. Now we have created the list view of str2 and added all the elements of str2 into the list. Again perform conversion from list to array and store the resultant array into str3 variable.
[A, E, I, O, U]
Java Stream API
The Stream.of() method of Stream interface returns a sequential ordered stream whose elements are the values.
Where MT is the type of stream elements. The method accepts values (elements of the new stream).
The flatMap() method is the method of Stream interface. It returns a stream consisting of the result.
Where R is the element type of new stream. The method accepts a mapper (a function to apply to each element which produces a stream of new values) as a parameter.
The toArray() method of Stream interface returns an array containing the elements of the stream.
Example of merging two arrays using Stream API
Merged array: [13, 12, 11, 6, 9, 3, 78, 34, 56, 67, 2, 11, 7]
Next TopicJava Tutorial
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week