- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Join Two Lists
In this article, we will understand how to join two lists. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.
Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.
Below is a demonstration of the same −
Suppose our input is −
First list is defined as: [Java, Python] Second list: [Mysql, Redshift]
The desired output would be −
The list after joining two strings: [Java, Python, Mysql, Redshift]
Algorithm
Step 1 - START Step 2 - Declare two lists namely input_list_1 and input_list_2 Step 3 - Define the values. Step 4 - Using the function join_lists() and passing both thelists as parametes, we join the two lists. Step 5 - Display the result Step 6 - 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<String> input_list_1 = new ArrayList<String>(); input_list_1.add("Java"); input_list_1.add("Python"); System.out.println("The first list is defined as: " + input_list_1); List<String> input_list_2 = new ArrayList<String>(); input_list_2.add("Mysql"); input_list_2.add("Redshift"); System.out.println("The second list is defined as: " + input_list_2); List<String> result_list = new ArrayList<String>(); result_list.addAll(input_list_1); result_list.addAll(input_list_2); System.out.println("\nThe list after joining two strings: " + result_list); } }
Output
The first list is defined as: [Java, Python] The second list is defined as: [Mysql, Redshift] The list after joining two strings: [Java, Python, Mysql, Redshift]
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 join_lists(List<String> input_list_1, List<String> input_list_2){ List<String> result_list = new ArrayList<String>(); result_list.addAll(input_list_1); result_list.addAll(input_list_2); System.out.println("\nThe list after joining two strings: " + result_list); } public static void main(String[] args) { List<String> input_list_1 = new ArrayList<String>(); input_list_1.add("Java"); input_list_1.add("Python"); System.out.println("The first list is defined as: " + input_list_1); List<String> input_list_2 = new ArrayList<String>(); input_list_2.add("Mysql"); input_list_2.add("Redshift"); System.out.println("The second list is defined as: " + input_list_2); join_lists(input_list_1, input_list_2); } }
Output
The first list is defined as: [Java, Python] The second list is defined as: [Mysql, Redshift] The list after joining two strings: [Java, Python, Mysql, Redshift]
- Related Articles
- Java program to join two given lists in Java
- How to join two lists in C#?
- Java Program to Merge two lists
- How to join or concatenate two lists in C#?
- Java program to find missing and additional values in two lists
- How to join list of lists in python?
- Java program to split the Even and Odd elements into two different lists
- C# program to concat two or more Lists
- C# program to find Intersection of two lists
- Python program to find Intersection of two lists?
- Java Program to format date time with Join
- Java program to split and join a string
- C# program to find additional values in two lists
- C# program to list the difference between two lists
- Python program to list the difference between two lists.

Advertisements