Javatpoint Logo
Javatpoint Logo

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
  • Without using arraycopy() method
  • Java Collections
  • Java Stream API

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.

Syntax:

Parameters

  • source: It is a source array.
  • source_position: Starting point in the source array.
  • destination: It is a destination array.
  • destination_position: Starting position in the destination array.
  • length: The number of array elements to be copied

It throws NullPointerException if the source or destination array is null. It also throws ArrayIndexOutOfBoundsException if:

  • source_position or destination_position or length is negative.
  • source_position+length is greater than the length of the source array, or destination_position+length is greater than the length of the destination array.

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.

Output:

[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.

Example

Output:

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.

Output:

[56, 78, 90, 32, 67, 12, 11, 14, 9, 5, 2, 23, 15]

Using Collections

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.

Output:

[A, E, I, O, U]

Java Stream API

Stream.of() method

The Stream.of() method of Stream interface returns a sequential ordered stream whose elements are the values.

Syntax

Where MT is the type of stream elements. The method accepts values (elements of the new stream).

flatMap() method

The flatMap() method is the method of Stream interface. It returns a stream consisting of the result.

Syntax

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.

toArray() method

The toArray() method of Stream interface returns an array containing the elements of the stream.

Syntax

Example of merging two arrays using Stream API

Output:

Merged array: [13, 12, 11, 6, 9, 3, 78, 34, 56, 67, 2, 11, 7]

Next TopicJava Tutorial




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA