Given two ArrayLists in Java, the task is to join these ArrayLists.
Examples:
Input: ArrayList1: [Geeks, For, ForGeeks], ArrayList2: [GeeksForGeeks, A computer portal]
Output: ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal]Input: ArrayList1: [G, e, e, k, s], ArrayList2: [F, o, r, G, e, e, k, s]
Output: ArrayList: [G, e, e, k, s, F, o, r, G, e, e, k, s]
Approach: ArrayLists can be joined in Java with the help of Collection.addAll() method. This method is called by the destination ArrayList and the other ArrayList is passed as the parameter to this method. This method appends the second ArrayList to the end of the first ArrayList.
Syntax:
ArrayList1.addAll(ArrayList2);
Below is the implementation of the above approach:
// Java program to demonstrate // How to sort ArrayList in descending order import java.util.*; public class GFG { public static void main(String args[]) { // Get the ArrayList1 ArrayList<String> list1 = new ArrayList<String>(); // Populate the ArrayList list1.add("Geeks"); list1.add("For"); list1.add("ForGeeks"); // Print the ArrayList 1 System.out.println("ArrayList 1: " + list1); // Get the ArrayList2 ArrayList<String> list2 = new ArrayList<String>(); list2.add("GeeksForGeeks"); list2.add("A computer portal"); // Print the ArrayList 2 System.out.println("ArrayList 2: " + list2); // Join the ArrayLists // using Collection.addAll() method list1.addAll(list2); // Print the joined ArrayList System.out.println("Joined ArrayLists: " + list1); } } |
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find common elements in two ArrayLists in Java
- Iterating over ArrayLists in Java
- Java Concurrency - yield(), sleep() and join() methods
- isAlive() and join() methods of Thread Class in Java
- Java String join() with examples
- Comparison of yield(), join() and sleep() in Java
- Differences between wait() and join() methods in Java
- Ints join() function | Guava | Java
- Java Guava | Booleans.join() method with Examples
- Java Guava | Shorts.join() method with Examples
- Java Guava | Longs.join() method with Examples
- Java Guava | Doubles.join() method with Examples
- Java Guava | Floats.join() method with Examples
- Java Guava | Chars.join() method with Examples
- How to compare two arrays in Java?
- Swap two Strings without using third user defined variable in Java
- Image Processing in Java | Set 14 ( Comparison of two images )
- Java program to merge two files into a third file
- Java program to merge two files alternatively into third file
- Java program to merge two files alternatively into third file
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

