How to Merge Two Arrays in JavaMerging 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() methodJava 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
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. 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() methodExample 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 CollectionsExample 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 APIStream.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
|
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