How to merge two arrays in Java?

To merge two arrays in Java, do this: Stream.of(array1, array2).flatMap(Stream::of).toArray().

Here's how you do it:

Integer[] array1 = new Integer[] { 1, 2, 3, 4, 5 };
Integer[] array2 = new Integer[] { 6, 7, 8, 9, 0 };

Integer[] merged = java.util.stream.Stream
  .of(array1, array2)
  .flatMap(java.util.stream.Stream::of)
  .toArray(Integer[]::new);

// [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
System.out.println(java.util.Arrays.toString(merged));

Primitive arrays

Use System.arraycopy for primitive arrays:

int[] array1 = new int[] { 1, 2, 3, 4, 5 };
int[] array2 = new int[] { 6, 7, 8, 9, 0 };

int[] merged = new int[array1.length + array2.length];
System.arraycopy(array1, 0, merged, 0, array1.length);
System.arraycopy(array2, 0, merged, array1.length, array2.length);

// [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
System.out.println(java.util.Arrays.toString(merged));