Java program to concatenate two arrays using Stream API

Program

import java.util.stream.Stream;   
import java.util.Arrays;
import java.util.Scanner;  
public class ConcatenateTwoArraysUsingStreamAPI {
  public static <T> Object[] mergeArray(T[] array1, T[] array2) {
    return Stream.of(array1, array2).flatMap(Stream::of).toArray();
  }
  public static void main(String[] args) {
    int i;
    System.out.println("Enter the required size of the first array: ");
    Scanner reader = new Scanner(System.in);
    int size = reader.nextInt();
    String inputFirstArray[] = new String[size];
    System.out.println("Enter the elements of the array: ");
    for (i = 0; i < inputFirstArray.length; i++) {
      inputFirstArray[i] = reader.next();
    }
    System.out.println("Enter the required size of the second array: ");
    size = reader.nextInt();
    String inputSecondArray[] = new String[size];
    System.out.println("Enter the elements of the array: ");
    for (i = 0; i < inputSecondArray.length; i++) {
      inputSecondArray[i] = reader.next();
    }
    Object[] mergedArray = mergeArray(inputFirstArray,inputSecondArray); 
    System.out.println("Arrays after merging: ");
    System.out.println(Arrays.toString(mergedArray));   
  }
}

This Java program demonstrates how to concatenate two arrays using the Stream API.

  1. Array Merging Method:

    • mergeArray(T[] array1, T[] array2) is a generic method that accepts two arrays of any type (T).
    • It uses the Stream API to merge the arrays:
      • Stream.of(array1, array2) creates a stream of the two arrays.
      • .flatMap(Stream::of) flattens the stream, combining the elements of both arrays into a single stream.
      • .toArray() converts the resulting stream back into an array of Object[].
  2. Input First Array:

    • The user specifies the size of the first array.
    • A String array inputFirstArray[] is created, and the user enters its elements.
  3. Input Second Array:

    • Similarly, the user specifies the size of the second array.
    • A String array inputSecondArray[] is created, and the user enters its elements.
  4. Array Merging:

    • The mergeArray method is called with the two arrays as arguments.
    • The returned Object[] contains the concatenated elements from both arrays.
  5. Output:

    • The Arrays.toString() method prints the merged array in a readable format.

Key Features:

  • Stream API:
    • Simplifies the merging process with a functional programming approach.
    • The flatMap method efficiently combines multiple streams into one.
  • Generic Method:
    • The mergeArray method works for arrays of any type, providing flexibility.
  • Dynamic Input:
    • The program supports arrays of any size and user-defined elements.

Example Explanation:

Input:

  • Array 1: ["Hello", "World"]
  • Array 2: ["Java", "Streams"]

Process:

  1. Stream.of(array1, array2) creates a stream of the two arrays.
  2. .flatMap(Stream::of) flattens the arrays into a single stream: ["Hello", "World", "Java", "Streams"].
  3. .toArray() converts the stream into an array.

Output:

  • Merged Array: ["Hello", "World", "Java", "Streams"]

Advantages:

  • Concise and Readable: The Stream API provides a one-liner solution for merging arrays.
  • Type Safety: The generic method ensures compatibility with different types of arrays.
  • Efficiency: Stream API operations are optimized for performance in Java.

Output

javac .\ConcatenateTwoArraysUsingStreamAPI.java
java ConcatenateTwoArraysUsingStreamAPI        
Enter the required size of the first array: 
3
Enter the elements of the array: 
oodlescoop
tutorials
programs
Enter the required size of the second array: 
2
Enter the elements of the array: 
recipes
travel
Arrays after merging: 
[oodlescoop, tutorials, programs, recipes, travel]