
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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]
- Related Articles
- Python Program to Merge Two Lists and Sort it
- Java Program to Join Two Lists
- Merge Two Sorted Lists in Python
- Program to merge K-sorted lists in Python
- Merge two sorted linked lists using C++.
- Java program to join two given lists in Java
- Merge K sorted linked lists in Java
- Program to merge in between linked lists in Python
- Java program to merge two files into a third file
- Python program to merge two Dictionaries
- C# program to merge two Dictionaries
- Golang program to merge two slices
- Python Program to Merge two Tuples
- Java program to find missing and additional values in two lists
- Java program to merge two or more files alternatively into third file

Advertisements