How to Merge Two Arrays Using Stream in Java 8

Introduction

In this guide, we will learn how to merge two arrays using Java 8's Stream API. We will discuss:

- merge two integer arrays

- merge two String arrays

- merge two arrays of custom objects

Problem Statement

Write a Java program that:

  • Takes two arrays as input.
  • Merges the two arrays into a single array.
  • Displays the merged array.

Example:

  • Input: int[] array1 = {1, 2, 3}, int[] array2 = {4, 5, 6}
  • Output: [1, 2, 3, 4, 5, 6]

Solution Steps

  1. Define Two Input Arrays: Provide two arrays that need to be merged.
  2. Use Java 8 Streams: Use Stream.concat() to merge the two arrays into one stream.
  3. Convert to Array: Collect the merged stream into a new array.
  4. Display the Result: Print the merged array.

Java Program to Merge Two Integer Arrays

import java.util.Arrays;
import java.util.stream.IntStream;

public class MergeArrays {
    public static void main(String[] args) {
        // Step 1: Define the input arrays
        int[] array1 = {1, 2, 3};
        int[] array2 = {4, 5, 6};

        // Step 2: Merge the arrays using Streams
        int[] mergedArray = IntStream.concat(Arrays.stream(array1), Arrays.stream(array2))
                .toArray();  // Convert the merged stream to an array

        // Step 3: Display the merged array
        System.out.println("Merged Array: " + Arrays.toString(mergedArray));
    }
}

Output

Merged Array: [1, 2, 3, 4, 5, 6]

Explanation

  • Arrays.stream(array1): Converts the first array into an IntStream.
  • Arrays.stream(array2): Converts the second array into an IntStream.
  • IntStream.concat(): Merges the two streams into one stream.
  • toArray(): Converts the merged stream back into an array.
  • Arrays.toString(): Prints the resulting merged array.

Java Program to Merge Two String Arrays

Let’s use the same method to merge two string arrays.

Example

import java.util.Arrays;
import java.util.stream.Stream;

public class MergeStringArrays {
    public static void main(String[] args) {
        // Step 1: Define the input arrays
        String[] array1 = {"Apple", "Banana", "Orange"};
        String[] array2 = {"Mango", "Grapes"};

        // Step 2: Merge the arrays using Streams
        String[] mergedArray = Stream.concat(Arrays.stream(array1), Arrays.stream(array2))
                .toArray(String[]::new);  // Convert the merged stream to an array

        // Step 3: Display the merged array
        System.out.println("Merged Array: " + Arrays.toString(mergedArray));
    }
}

Output

Merged Array: [Apple, Banana, Orange, Mango, Grapes]

Explanation

  • Arrays.stream(array1): Converts the first string array into a stream.
  • Arrays.stream(array2): Converts the second string array into a stream.
  • Stream.concat(): Merges the two streams into one.
  • toArray(String[]::new): Converts the merged stream back into a string array.

Java Program to Merge Two Arrays of Custom Objects (Using Employee Class)

In this example, we will merge two arrays of Employee objects.

Example

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class MergeEmployeeArrays {
    public static void main(String[] args) {
        // Step 1: Define two input arrays of Employee objects
        Employee[] employees1 = {
                new Employee("John", 55000),
                new Employee("Alice", 48000)
        };

        Employee[] employees2 = {
                new Employee("Bob", 60000),
                new Employee("Jane", 45000)
        };

        // Step 2: Merge the two arrays using Streams
        Employee[] mergedEmployees = Stream.concat(Arrays.stream(employees1), Arrays.stream(employees2))
                .toArray(Employee[]::new);  // Convert the merged stream to an array

        // Step 3: Display the merged array of Employee objects
        System.out.println("Merged Employees: " + Arrays.toString(mergedEmployees));
    }
}

// Employee class definition
class Employee {
    private String name;
    private int salary;

    public Employee(String name, int salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return name + " (Salary: " + salary + ")";
    }
}

Output

Merged Employees: [John (Salary: 55000), Alice (Salary: 48000), Bob (Salary: 60000), Jane (Salary: 45000)]

Explanation

  • Stream.concat(Arrays.stream(employees1), Arrays.stream(employees2)): Merges the two streams of Employee objects.
  • toArray(Employee[]::new): Converts the merged stream back into an Employee array.
  • The Employee class has an overridden toString() method to display the object details.

Conclusion

In Java 8, merging two arrays is straightforward using the Stream API. By using Stream.concat(), you can merge arrays of primitive types (like integers), strings, or even custom objects (like Employee). This approach is clean, efficient, and makes use of modern Java features.

Comments