Merging and sorting two arrays of strings is a common programming task, especially in scenarios like data processing, lexicographic ordering, and database query results. In this article, we will write a Java program to merge two string arrays and sort the combined array alphabetically.
Understanding the Problem
We are given two separate arrays of strings and need to:
- Combine both arrays into a single array.
- Sort the merged array alphabetically (lexicographically).
Example:
Input:
String[] array1 = {“banana”, “apple”, “grape”};
String[] array2 = {“orange”, “peach”, “banana”};
Output:
[“apple”, “banana”, “banana”, “grape”, “orange”, “peach”]
Approach to Solve the Problem
- Merge the two arrays – Use System.arraycopy() or a List for flexibility.
- Sort the merged array – Use Arrays.sort() or Collections.sort().
- Return the sorted array.
1. Using System.arraycopy() and Arrays.sort() (Efficient Approach)
package com.javacodepoint.stringarray;
import java.util.Arrays;
public class MergeAndSortStrings {
// Method to merge and sort two string arrays
public static String[] mergeAndSortArrays(String[] array1, String[] array2) {
int mergedLength = array1.length + array2.length;
String[] mergedArray = new String[mergedLength];
// Copy elements from both arrays
System.arraycopy(array1, 0, mergedArray, 0, array1.length);
System.arraycopy(array2, 0, mergedArray, array1.length, array2.length);
// Sort the merged array alphabetically
Arrays.sort(mergedArray);
return mergedArray;
}
public static void main(String[] args) {
String[] array1 = { "banana", "apple", "grape" };
String[] array2 = { "orange", "peach", "banana" };
// Merge and sort arrays
String[] sortedArray = mergeAndSortArrays(array1, array2);
System.out.println("Merged and Sorted Array: " + Arrays.toString(sortedArray));
}
}
2. Alternative Approach: Using ArrayList and Collections.sort()
Another way is to use an ArrayList, which makes merging dynamic and flexible.
package com.javacodepoint.stringarray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MergeSortUsingList {
public static void main(String[] args) {
String[] array1 = { "banana", "apple", "grape" };
String[] array2 = { "orange", "peach", "banana" };
// Convert arrays to a List and merge
List<String> mergedList = new ArrayList<>(Arrays.asList(array1));
mergedList.addAll(Arrays.asList(array2));
// Sort the list
Collections.sort(mergedList);
// Convert back to array and print
String[] sortedArray = mergedList.toArray(new String[0]);
System.out.println("Merged and Sorted Array: " + Arrays.toString(sortedArray));
}
}
Conclusion
This article covered how to merge and sort two arrays of strings using two different methods:
- Using
System.arraycopy()
andArrays.sort()
(Efficient & Fast) - Using
ArrayList
andCollections.sort()
(Flexible & Easy-to-Read)
These methods are useful in scenarios like sorting user input, database query results, or natural language processing.
You can learn the Top 20 string array programs for interview preparation (Click here).