Write a Java Program to Join Two Lists

In Java, we can easily join two lists using the addAll() method provided by the List interface.

This method appends all the elements of the specified collection to the end of the list.

Let’s see how we can use this method to join two lists in Java.


First, we need to create two lists that we want to join.

We can create these lists using the ArrayList class in Java.

Here is an example code snippet to create two lists:

List<String> list1 = new ArrayList<>();
list1.add("apple");
list1.add("banana");
list1.add("orange");

List<String> list2 = new ArrayList<>();
list2.add("grape");
list2.add("kiwi");
list2.add("mango");

In this example, we have created two lists, list1 and list2, and added some elements to them.

To join these two lists, we can use the addAll() method of List interface.

Here is an example code snippet that shows how to join two lists:

List<String> joinedList = new ArrayList<>();
joinedList.addAll(list1);
joinedList.addAll(list2);

In this example, we have created a new list called joinedList and added all the elements of list1 and list2 using the addAll() method.

Now we have successfully joined two lists into one list called joinedList.

We can access the elements of this joined list using the standard List methods, such as get() and size().

Here is the complete Java program that joins two lists:

import java.util.ArrayList;
import java.util.List;

public class JoinListsExample {
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<>();
        list1.add("apple");
        list1.add("banana");
        list1.add("orange");

        List<String> list2 = new ArrayList<>();
        list2.add("grape");
        list2.add("kiwi");
        list2.add("mango");

        List<String> joinedList = new ArrayList<>();
        joinedList.addAll(list1);
        joinedList.addAll(list2);

        System.out.println("Joined List: " + joinedList);
    }
}

In this program, we have created list1 and list2, joined them using the addAll() method, and printed the joined list using the System.out.println() method.

That’s it! This is how we can join two lists in Java using the addAll() method.

There are some other ways to join two lists as well, but using the addAll() method is the easiest and most efficient way.

Another approach to join two lists is to use the Stream API provided by Java 8 or later versions.

We can use the Stream.concat() method to concatenate two lists.

Here’s an example:

List<String> joinedList = Stream.concat(list1.stream(), list2.stream())
                                .collect(Collectors.toList());

In this example, we have used the Stream.concat() method to concatenate list1 and list2 and then collected the result in a new list using the Collectors.toList() method.

This approach may be useful when we want to apply some transformations or filters to the elements of the list before joining them.


In conclusion, joining two lists in Java is a simple task.

We can use the addAll() method of the List interface or the Stream.concat() method provided by the Stream API to join two lists efficiently.

The choice of method depends on the specific requirements of the use case.