Java Program to Join Two Lists

Java Program to Join Two Lists

What is an List in Java ?

In Java, a list is a collection of elements of the same type, which are ordered and can be accessed using an index. A list is a part of the Java Collection Framework and is implemented by the List interface.
There are several classes in Java that implement the List interface, such as ArrayList, LinkedList, and Vector. These classes provide different ways of storing and manipulating lists of elements, but they all have common methods defined in the List interface, such as adding and removing elements, getting the size of the list, accessing elements by index, and iterating over the elements in the list.

Ways to Add two Lists in Java :

Syntax :

List list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);

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

list1.addAll(list2); // add all elements of list2 to list1
System.out.println(list1); // Output: [1, 2, 3, 4, 5, 6]

Syntax :

List list = new ArrayList<>();
String[] array = {"one", "two", "three"};
Collections.addAll(list, array); // add all elements of array to list
System.out.println(list); // Output: [one, two, three]

Syntax :

String[] array = {"one", "two", "three"};
List list = Arrays.asList(array); // convert array to list
System.out.println(list); // Output: [one, two, three]

Example 1 : Using addAll() and Collections.addAll() Method :

Run

import java.util.*;

public class Main {

    public static void main(String[] args) {
        // Creating the first list
        List< Integer > list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        System.out.println("List 1: " + list1);

        // Creating the second list
        List< Integer > list2 = new ArrayList<>();
        list2.add(4);
        list2.add(5);
        list2.add(6);
        System.out.println("List 2: " + list2);

        // Adding all elements of the second list to the first list using addAll() method
        list1.addAll(list2);
        System.out.println("After adding list2 to list1 using addAll() method: " + list1);

        // Creating an array of strings
        String[] array = {"prepinsta", "prime", "prepinstaprime"};

        // Creating a new list and adding all elements of the array to it using Collections.addAll() method
        List< String > list3 = new ArrayList<>();
        Collections.addAll(list3, array);
        System.out.println("List 3: " + list3);
    }
}

Output :

List 1: [1, 2, 3]
List 2: [4, 5, 6]
After adding list2 to list1 using addAll() method: [1, 2, 3, 4, 5, 6]
List 3: [prepinsta, prime, prepinstaprime]

Example 2 : Using the Arrays.asList() method: 

Run
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        // Creating the first list
        List< Integer > list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        System.out.println("List 1: " + list1);

        // Creating the second list
        List< Integer > list2 = new ArrayList<>();
        list2.add(4);
        list2.add(5);
        list2.add(6);
        System.out.println("List 2: " + list2);

        // Using the Arrays.asList() method to add both lists
        List < List < Integer >> listOfLists = new ArrayList<>();
        listOfLists.add(list1);
        listOfLists.add(list2);
        List < Integer > flattenedList = new ArrayList<>();
        for (List < Integer > list : listOfLists) {
            flattenedList.addAll(list);
        }
        System.out.println("Flattened list: " + flattenedList);
    }
}

Output :

List 1: [1, 2, 3]
List 2: [4, 5, 6]
Flattened list: [1, 2, 3, 4, 5, 6]

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription