The Wayback Machine - https://web.archive.org/web/20250116090726/https://www.geeksforgeeks.org/join-two-arraylists-in-java/
Open In App

Join two ArrayLists in Java

Last Updated : 16 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given two ArrayLists in Java, the task is to join these ArrayLists.

Examples:

Input: ArrayList1: [Geeks, For, ForGeeks] , ArrayList2: [GeeksForGeeks, A computer portal]
Output: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal]

Input: ArrayList1: [G, e, e, k, s] , ArrayList2: [F, o, r, G, e, e, k, s]
Output: [G, e, e, k, s, F, o, r, G, e, e, k, s]

Syntax:

ArrayList1.addAll(ArrayList2);

Implementation:

Java
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("Geeks");

        // Get the ArrayList2
        ArrayList<String> list2 = new ArrayList<String>();

        list2.add("GeeksForGeeks");
        list2.add("A Computer Science Portal");

        // Join the ArrayLists
        // using Collection.addAll() method
        list1.addAll(list2);

        // Print the joined ArrayList
        System.out.println(list1);
    }
}

Output
[Geeks, for, Geeks, GeeksForGeeks, A Computer Science Portal]

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.

Start your Java programming journey today with our Java Programming Online Course, designed for both beginners and advanced learners. With self-paced lessons covering everything from basic syntax to advanced concepts, you’ll gain the skills needed to excel in the world of programming.

Take the Three 90 Challenge! Complete 90% of the course in 90 days, and earn a 90% refund. Track your progress and stay motivated as you master Java.
Join now and start your path to becoming a Java expert!


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg