Java Program To Merge Two Lists

Chapter: Miscellaneous Last Updated: 17-05-2023 15:08:38 UTC


            /* ............... START ............... */
import java.util.ArrayList;
import java.util.List;

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

        List<Integer> list2 = new ArrayList<>();

        // Merge the lists
        List<Integer> mergedList = new ArrayList<>();

        // Print the merged list
        System.out.println("Merged list: " + mergedList);

                /* ............... END ............... */


Merged list: [1, 2, 3, 4, 5, 6]


  • In this example, we create two lists called list1 and list2. list1 contains the numbers 1, 2, and 3, while list2 contains the numbers 4, 5, and 6.
  • We then create a new list called mergedList, which will hold the merged result.
  • To merge the lists, we use the addAll() method. We call mergedList.addAll(list1) to add all the elements of list1 to mergedList, and then we call mergedList.addAll(list2) to add all the elements of list2 to mergedList. This concatenates the elements from both lists into the mergedList.
  • Finally, we print the merged list using System.out.println(), which displays the merged list as [1, 2, 3, 4, 5, 6].
  • So, the program demonstrates how to merge two lists by using the addAll() method in Java.


Java Program To Merge Two Lists # How to Merge Two Lists in Java #Java merge two arrays

Similar Programs Chapter Last Updated
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023
Java Program To Run Something Every X Minutes Miscellaneous 22-04-2023
Java Program To Reverse A String Miscellaneous 22-04-2023
Java Program To Implement Method Overloading Miscellaneous 16-04-2023
Java Parse JSON String To Object Miscellaneous 24-03-2023
Java Program For Date And Time Miscellaneous 24-03-2023
Java Binary Search Tree Implementation Miscellaneous 21-03-2023
Java Program To Merge Two PDF Files Miscellaneous 18-03-2023
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Currency Formatter In Java | How To Format Currency In Java Miscellaneous 19-07-2021
Factory Design Pattern In Java Miscellaneous 11-05-2021
Data Types In Java Miscellaneous 09-06-2018