Java Program to Merge two lists


In this article, we will understand how to merge two lists. A list is an ordered collection that allows us to store and access elements sequentially. It contains the index-based methods to insert, update, delete and search the elements. It can also have the duplicate elements.

Below is a demonstration of the same −

Suppose our input is

First list: [45, 60, 95]
Second list: [105, 120]

The desired output would be

The list after merging the two lists: [45, 60, 95, 105, 120]

Algorithm

Step 1 - START
Step 2 - Declare three integer lists namely input_list_1, input_list_2 and result_list.
Step 3 - Define the values.
Step 4 - Use result_list.addAll(input_list_1) to add all the elements of the input_list_1 to the result list.
Step 5 - Use result_list.addAll(input_list_2) to add all the elements of the input_list_2 to the result list.
Step 6 - Display the result_list.
Step 7 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.util.ArrayList;
import java.util.List;
public class Demo {
   public static void main(String[] args) {
      List<Integer> input_list_1 = new ArrayList<>();
      input_list_1.add(45);
      input_list_1.add(60);
      input_list_1.add(95);
      System.out.println("The first list is defined as: " + input_list_1);
      List<Integer> input_list_2 = new ArrayList<>();
      input_list_2.add(105);
      input_list_2.add(120);
      System.out.println("The second list is defined as: " + input_list_2);
      List<Integer> result_list = new ArrayList<>();
      result_list.addAll(input_list_1);
      result_list.addAll(input_list_2);
      System.out.println("\nThe list after merging the two lists: " + result_list);
   }
}

Output

The first list is defined as: [45, 60, 95]
The second list is defined as: [105, 120]

The list after merging the two lists: [45, 60, 95, 105, 120]

Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

import java.util.ArrayList;
import java.util.List;
public class Demo {
   static void merge(List<Integer> input_list_1, List<Integer> input_list_2){
      List<Integer> result_list = new ArrayList<>();
      result_list.addAll(input_list_1);
      result_list.addAll(input_list_2);
      System.out.println("\nThe list after merging the two lists: " + result_list);
   }
   public static void main(String[] args) {
      List<Integer> input_list_1 = new ArrayList<>();
      input_list_1.add(45);
      input_list_1.add(60);
      input_list_1.add(95);
      System.out.println("The first list is defined as: " + input_list_1);
      List<Integer> input_list_2 = new ArrayList<>();
      input_list_2.add(105);
      input_list_2.add(120);
      System.out.println("The second list is defined as: " + input_list_2);
      merge(input_list_1, input_list_2);
   }
}

Output

The first list is defined as: [45, 60, 95]
The second list is defined as: [105, 120]

The list after merging the two lists: [45, 60, 95, 105, 120]

Updated on: 30-Mar-2022

324 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements