Java Program to Concatenate Two List
Concatenating two lists means merging two lists into a single list.
Consider the given lists:
LIST 1
LIST 2
LIST AFTER CONCATENATION
There are several methods to perform concatenation operation:
- Using addAll() method
- Using stream
- Using union()
Method 1: Using addAll() method
Syntax:
addAll ( list name )
This method takes name of list as argument and add all the elements of the specified list in the same order as the original list.
- Create a new empty list ( concatenated_list)
- Use addAll () method to concatenate the given list1 and list2 into the newly created list.
concatenated_list.addAll (list1) // concatenates first list
concatenated_list.addAll (list2) // concatenates second list
After performing the above steps our empty list now contains both the list.
Java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class GFG {
public static void main(String[] args)
{
List<Integer> list1 = new ArrayList<Integer>();
list1.add( 1 );
list1.add( 2 );
list1.add( 3 );
list1.add( 4 );
List<Integer> list2 = new ArrayList<Integer>();
list2.add( 5 );
list2.add( 6 );
list2.add( 7 );
list2.add( 8 );
List<Integer> concatenated_list
= new ArrayList<Integer>();
concatenated_list.addAll(list1);
concatenated_list.addAll(list2);
System.out.println( "list1: " + list1);
System.out.println( "list2: " + list2);
System.out.println( "Concatenated list: "
+ concatenated_list);
}
}
|
Output
list1: [1, 2, 3, 4]
list2: [5, 6, 7, 8]
Concatenated list: [1, 2, 3, 4, 5, 6, 7, 8]
Method 2: Using streams
Stream.concat(list1.stream(),list2.stream()).collect(Collectors.toList())
It takes two streams as argument and creates a concatenated stream out of them.Second list is appended to the first list.
Java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GFG {
public static void main(String[] args)
{
List<Integer> list1 = new ArrayList<Integer>();
list1.add( 1 );
list1.add( 2 );
list1.add( 3 );
list1.add( 4 );
List<Integer> list2 = new ArrayList<Integer>();
list2.add( 5 );
list2.add( 6 );
list2.add( 7 );
list2.add( 8 );
List<Integer> concatenated_list = new ArrayList<Integer>();
concatenated_list = Stream.concat(list1.stream(), list2.stream())
.collect(Collectors.toList());
System.out.println( "list1: " + list1);
System.out.println( "list2: " + list2);
System.out.println( "Concatenated list: " + concatenated_list);
}
}
|
Output
list1: [1, 2, 3, 4]
list2: [5, 6, 7, 8]
Concatenated list: [1, 2, 3, 4, 5, 6, 7, 8]
Method 3: Using union()
ListUtils.union( list1, list2)
It takes two list as argument and return new concatenated list. Second list is appended to the first list.
Java
import java.io.*;
import java.util.*;
import java.lang.*;
import org.apache.commons.collections.ListUtils;
public class GFG {
public static void main(String[] args)
{
List<Integer> list1 = new ArrayList<Integer>();
list1.add( 1 );
list1.add( 2 );
list1.add( 3 );
list1.add( 4 );
List<Integer> list2 = new ArrayList<Integer>();
list2.add( 5 );
list2.add( 6 );
list2.add( 7 );
list2.add( 8 );
List<Integer> concatenated_list = new ArrayList<Integer>();
concatenated_list = ListUtils.union(list1, list2);
System.out.println( "list1: " + list1);
System.out.println( "list2: " + list2);
System.out.println( "Concatenated list: "
+ concatenated_list);
}
}
|
Output:
Prerequisites for running the above code:
Last Updated :
19 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...