Introduction
Merging two arrays is a common operation in programming, particularly when you need to combine data from different sources or consolidate lists. In Java, merging arrays can be done in several ways, but Java 8 introduces a more elegant and functional approach using Streams. This guide will show you how to merge two string arrays into a single array using Java 8 Streams.
Problem Statement
The task is to create a Java program that:
- Accepts two string arrays as input.
- Uses Java 8 Streams to merge the two arrays into one.
- Outputs the merged array.
Example 1:
- Input:
["apple", "banana"]and["orange", "grape"] - Output:
["apple", "banana", "orange", "grape"]
Example 2:
- Input:
["Java", "Python"]and["C++", "JavaScript"] - Output:
["Java", "Python", "C++", "JavaScript"]
Solution Steps
- Input Arrays: Start with two string arrays that can either be hardcoded or provided by the user.
- Stream Processing: Convert both arrays into streams, concatenate them, and then collect the result into a single array.
- Display the Result: Print the merged array.
Java Program
Java 8: Merge Two String Arrays
import java.util.Arrays;
import java.util.stream.Stream;
/**
* Java 8: Merge Two String Arrays
* Author: https://www.rameshfadatare.com/
*/
public class MergeStringArrays {
public static void main(String[] args) {
// Step 1: Take input string arrays
String[] array1 = {"apple", "banana"};
String[] array2 = {"orange", "grape"};
// Step 2: Merge the two arrays using streams
String[] mergedArray = mergeArrays(array1, array2);
// Step 3: Display the result
System.out.println(Arrays.toString(mergedArray));
}
// Method to merge two string arrays
public static String[] mergeArrays(String[] array1, String[] array2) {
return Stream.concat(Arrays.stream(array1), Arrays.stream(array2))
.toArray(String[]::new);
}
}
Explanation of the Program
-
Input Handling: The program uses two string arrays
{"apple", "banana"}and{"orange", "grape"}as example inputs. This can be modified to accept input from the user if required. -
Stream Processing: The
Arrays.stream(array1)andArrays.stream(array2)methods convert each array into a stream. TheStream.concat()method is then used to merge the two streams into one. -
Collecting the Result: The merged stream is collected back into an array using the
toArray(String[]::new)method, which converts the stream into a new string array. -
Output: The program prints the merged array using
Arrays.toString().
Output Example
Example 1:
Input: ["apple", "banana"] and ["orange", "grape"]
Output: ["apple", "banana", "orange", "grape"]
Example 2:
Input: ["Java", "Python"] and ["C++", "JavaScript"]
Output: ["Java", "Python", "C++", "JavaScript"]
Advanced Considerations
-
Handling Null Values: If any of the arrays contain null values, they will be included in the merged array. If you want to filter out null values, you can add a
filter(Objects::nonNull)step in the stream processing. -
Duplicate Values: The merged array will contain all elements from both arrays, including duplicates. If you want to remove duplicates, you can use
distinct()before collecting the result. -
Performance Considerations: This approach is efficient for merging arrays of typical sizes and leverages the functional programming features of Java 8. The use of streams provides a clear and concise method for merging arrays.
Conclusion
This Java 8 program efficiently merges two string arrays using streams. By leveraging the power of the Stream API, the solution is both concise and powerful, making it suitable for various data processing tasks. Whether you’re consolidating data from different sources or combining lists for further processing, this method provides an effective approach to merging arrays in Java.