Concatenating two lists means merging two lists into a single list.
Consider the given lists:
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 Program to Concatenate Two List // using addAll() method import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class GFG {
public static void main(String[] args)
{
// given list 1
List<Integer> list1 = new ArrayList<Integer>();
list1.add( 1 );
list1.add( 2 );
list1.add( 3 );
list1.add( 4 );
// given list 2
List<Integer> list2 = new ArrayList<Integer>();
list2.add( 5 );
list2.add( 6 );
list2.add( 7 );
list2.add( 8 );
// creating new empty list
List<Integer> concatenated_list
= new ArrayList<Integer>();
// using addAll( ) method to concatenate the lists
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);
}
} |
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 Program to Concatenate Two List // using streams 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)
{
// given list 1
List<Integer> list1 = new ArrayList<Integer>();
list1.add( 1 );
list1.add( 2 );
list1.add( 3 );
list1.add( 4 );
// given list 2
List<Integer> list2 = new ArrayList<Integer>();
list2.add( 5 );
list2.add( 6 );
list2.add( 7 );
list2.add( 8 );
// creating new empty list
List<Integer> concatenated_list = new ArrayList<Integer>();
// using Stream.concat() method to concatenate the lists
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);
}
} |
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 program to concatenate two lists // using union() import java.io.*;
import java.util.*;
import java.lang.*;
import org.apache.commons.collections.ListUtils;
public class GFG {
public static void main(String[] args)
{
// given list 1
List<Integer> list1 = new ArrayList<Integer>();
list1.add( 1 );
list1.add( 2 );
list1.add( 3 );
list1.add( 4 );
// given list 2
List<Integer> list2 = new ArrayList<Integer>();
list2.add( 5 );
list2.add( 6 );
list2.add( 7 );
list2.add( 8 );
// creating new empty list
List<Integer> concatenated_list = new ArrayList<Integer>();
// using ListUtils.union() method to concatenate
// the lists
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:
- Download and Install the following Apache Commons Collections library http://commons.apache.org/proper/commons-collections/download_collections.cgi
- Now go to the properties of your project.
- Select Java Build Path.
- Under Java Build Path go to libraries.
- Under libraries select Classpath.
- Now go to add external JARs and add the JAR executable file, downloaded from the given link.