Java program to join two lists

To join two lists in Java, you can use the addAll method of the ArrayList class. Here's an example program that demonstrates this:

re‮gi:ot ref‬iftidea.com
import java.util.ArrayList;
import java.util.List;

public class JoinLists {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);

        List<Integer> list2 = new ArrayList<>();
        list2.add(4);
        list2.add(5);
        list2.add(6);

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

        System.out.println("List 1: " + list1);
        System.out.println("List 2: " + list2);
        System.out.println("Joined List: " + joinedList);
    }
}

In this program, we create two ArrayList objects, list1 and list2, and populate them with some values. We then create a new ArrayList called joinedList and add all the elements of list1 to it using the addAll method. Finally, we add all the elements of list2 to joinedList using the addAll method.

After joining the two lists, we print out the original lists and the joined list to the console using the System.out.println method.

When you run the program, the output will be:

List 1: [1, 2, 3]
List 2: [4, 5, 6]
Joined List: [1, 2, 3, 4, 5, 6]

As you can see, the program successfully joins the two lists and creates a new list that contains all the elements of the original lists.