- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Merge arrays into a new object array in Java
Following is the Java program to merge array into a new object array in Java −
Example
import java.util.stream.Stream; import java.util.Arrays; import java.io.*; public class Demo{ public static <T> Object[] concat_fun(T[] my_obj_1, T[] my_obj_2){ return Stream.concat(Arrays.stream(my_obj_1), Arrays.stream(my_obj_2)).toArray(); } public static void main (String[] args){ Integer[] my_obj_1 = new Integer[]{67, 83, 90}; Integer[] my_obj_2 = new Integer[]{11, 0, 56}; Object[] my_obj_3 = concat_fun(my_obj_1,my_obj_2); System.out.println("The two objects merged into a single object array : " + Arrays.toString(my_obj_3)); } }
Output
The two objects merged into a single object array : [67, 83, 90, 11, 0, 56]
A class named Demo contains the ‘concat_fun’ that takes two objects and returns the concatenated objects as output. In the main string, two integer objects are created and a third object is used to store the concatenated objects. The relevant messages are displayed on the console.
Advertisements