Write a Java Program to Merge two lists

In Java, we can easily merge two lists using the addAll() method.

The addAll() method is a built-in method of the List interface that is used to add all the elements of one list to another list.

In this tutorial, we will discuss how to merge two lists in Java using the addAll() method.


Here’s a Java program that demonstrates how to merge two lists using the addAll() method:

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

public class MergeListsExample {
    public static void main(String[] args) {
        // Create two lists
        List<String> list1 = new ArrayList<>();
        list1.add("apple");
        list1.add("banana");

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

        // Merge two lists
        list1.addAll(list2);

        // Print merged list
        System.out.println(list1);
    }
}

In this program, we have created two lists list1 and list2 and added some elements to them using the add() method.

Then, we have merged the two lists using the addAll() method, which adds all the elements of list2 to list1.

Finally, we have printed the merged list using the println() method.

The output of this program will be:

[apple, banana, orange, kiwi]

As you can see, the two lists have been merged into one list.

In addition to the addAll() method, we can also use the List interface’s add() method to add individual elements of one list to another list.

However, the addAll() method is more efficient when it comes to merging two lists, as it adds all the elements of one list to another list in one step.

In conclusion, merging two lists in Java is a simple task that can be accomplished using the addAll() method of the List interface.

The addAll() method adds all the elements of one list to another list, resulting in a merged list.

Additionally, we can also use the Collections class to merge two lists in Java.

The Collections class is a utility class in Java that provides a set of static methods for operating on collections.

Here’s an example Java program that demonstrates how to merge two lists using the Collections class:

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

public class MergeListsExample {
    public static void main(String[] args) {
        // Create two lists
        List<String> list1 = new ArrayList<>();
        list1.add("apple");
        list1.add("banana");

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

        // Merge two lists
        List<String> mergedList = new ArrayList<>(list1);
        mergedList.addAll(list2);

        // Or use Collections.addAll() method to merge two lists
        // List<String> mergedList = new ArrayList<>(list1);
        // Collections.addAll(mergedList, list2.toArray(new String[list2.size()]));

        // Print merged list
        System.out.println(mergedList);
    }
}

In this program, we have used the Collections class to merge two lists.

First, we have created two lists list1 and list2 and added some elements to them using the add() method.

Then, we have merged the two lists using the addAll() method of the List interface, which adds all the elements of list2 to list1.

Alternatively, we can use the Collections.addAll() method to merge two lists, which adds all the elements of the second list to the first list.

Finally, we have printed the merged list using the println() method.

The output of this program will be the same as the previous example:

[apple, banana, orange, kiwi]

In summary, there are two ways to merge two lists in Java: using the addAll() method of the List interface or using the Collections.addAll() method of the Collections class.

Both methods are efficient and straightforward to use.