Join two List Object using Java Program

In this tutorial, I am Just sharing the program snippet about Join two List Object using Java Program. The example java program has been tested with environment and output is shared in the same post.

JoinTwoList.java

package com.dineshkrish;
import java.util.ArrayList;
import java.util.List;
public class JoinTwoList {
public static void main(String[] args) {
// Defining first ArrayList Object 
List<String> list1 = new ArrayList<String>();
list1.add("One");
list1.add("Two");
list1.add("Three");
list1.add("Four");
list1.add("Five");
// Defining second ArrayList Object
List<String> list2 = new ArrayList<String>();
list2.add("Six");
list2.add("Seven");
list2.add("Eight");
list2.add("Nine");
list2.add("Ten");
System.out.println("Before Joining the List");
System.out.println("------------------------");
System.out.println("List 1 : "+list1);
System.out.println("List 2 : "+list2);
System.out.println("\nAfter Joining the List");
System.out.println("-----------------------");
// Joining Two Array List Object using 'addAll()' method
list1.addAll(list2);
System.out.println("List 1 & 2 : "+list1);
}
}

Output

—————-
Before Joining the List
————————
List 1 : [One, Two, Three, Four, Five]
List 2 : [Six, Seven, Eight, Nine, Ten]

After Joining the List
———————–
List 1 & 2 : [One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten]

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *