Merging two unsorted arrays in sorted order using java 8.

There are following way to Merging two unsorted arrays in sorted order using java 8.

  • we convert the array into Stream using Arrays.stream() method.
  • User IntStream.concat() method that concatenates two Arrays.stream() value into a single IntStream.
  • distinct method allow only unique data.
  • sorted method allow sort the data in ascending order.
  • Print the sortedArray using Array.toString() method.
import java.util.Arrays;
import java.util.stream.IntStream;

public class MergeTwoUnsortedArray {
	public static void main(String[] args) {
		int[] a = { 3, 6, 8, 10, 10 };
		int[] b = { 20, 15, 5};
		int[] sortedArray = IntStream.concat(Arrays.stream(a), Arrays.stream(b)).distinct().sorted().toArray();
		System.out.println("Merging two unsorted arrays :- " + Arrays.toString(sortedArray));
	}
}

Output :-
Merging two unsorted arrays :- [3, 5, 6, 8, 10, 15, 20]