Combine two arrays of different types in Java
This post will discuss how to combine two arrays of different types into a single new object array in Java. The new array should contain all the first array elements, followed by all the second array elements.
1. Using Stream API
We can combine two arrays of different types in Java using Stream.flatMap() method. The idea is to first create a stream of each array using the Stream.of() method and use the Stream.flatMap() method to combine two streams into one. Finally, we can use the Stream.toArray() method to convert the stream into an object array. Here is an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; import java.util.stream.Stream; class Main { // Generic method to combine two arrays of different types public static <T, U> Object[] combine(T[] first, U[] second) { return Stream.of(first, second) // Stream<Object[]> .flatMap(Stream::of) // Stream<Object> .toArray(); } public static void main(String[] args) { // The two arrays of different types to combine Integer[] first = { 1, 2, 3 }; String[] second = { "a", "b", "c" }; Object[] objects = combine(first, second); System.out.println(Arrays.toString(objects)); // [1, 2, 3, a, b, c] } } |
Alternatively, we can create a stream of each array using the Stream.of() method and use the Stream.concat() method to concatenate two streams into one. Finally, we can use the Stream.toArray() method to convert the stream into an array. Here is an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; import java.util.stream.Stream; class Main { // Generic method to combine two arrays of different types public static <T, U> Object[] combine(T[] first, U[] second) { return Stream.concat(Stream.of(first), Stream.of(second)) .toArray(); } public static void main(String[] args) { // The two arrays of different types to combine Integer[] first = { 1, 2, 3 }; String[] second = { "a", "b", "c" }; Object[] objects = combine(first, second); System.out.println(Arrays.toString(objects)); // [1, 2, 3, a, b, c] } } |
2. Using System.arraycopy() method
Another option is to use System.arraycopy() method to combine two arrays of different types in Java. The idea is to first create an array of objects that can store both types of elements. Then, we can use the System.arraycopy() method to copy the elements from the source arrays to the destination array. Here is an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; class Main { // Generic method to combine two arrays of different types public static <T, U> Object[] combine(T[] first, U[] second) { Object[] objects = new Object[first.length + second.length]; System.arraycopy(first, 0, objects, 0, first.length); System.arraycopy(second, 0, objects, first.length, second.length); return objects; } public static void main(String[] args) { // Two arrays of different types to combine Integer[] first = { 1, 2, 3 }; String[] second = { "a", "b", "c" }; Object[] objects = combine(first, second); System.out.println(Arrays.toString(objects)); // [1, 2, 3, a, b, c] } } |
3. Using Collections.addAll() method
We can also use the Collections.addAll() method to create a collection of objects for holding both types of elements. Here is an example of combining an array of String and an array of Integer into a List<Object> using Collections.addAll() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; class Main { // Generic method to combine two arrays of different types public static <T, U> Object[] combine(T[] first, U[] second) { List<Object> list = new ArrayList<>(Arrays.asList(first)); Collections.addAll(list, second); return list.toArray(); } public static void main(String[] args) { // Two arrays of different types to combine Integer[] first = { 1, 2, 3 }; String[] second = { "a", "b", "c" }; Object[] objects = combine(first, second); System.out.println(Arrays.toString(objects)); // [1, 2, 3, a, b, c] } } |
4. Using Guava’s ObjectArrays.concat() method
Guava provides a simple and convenient way to combine two arrays of different types in Java using the ObjectArrays.concat() method. This method takes two arrays as arguments and returns a new array containing the elements of both arrays. Here is an example of its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.ObjectArrays; import java.util.Arrays; class Main { public static void main(String[] args) { // Two arrays of different types to combine Integer[] first = { 1, 2, 3 }; String[] second = { "a", "b", "c" }; Object[] objects = ObjectArrays.concat(first, second, Object.class); System.out.println(Arrays.toString(objects)); // [1, 2, 3, a, b, c] } } |
That’s all about combining two arrays of different types in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)