Java Program to Concatenate Two List
Last Updated :
19 Jan, 2021
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
// 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);
}
}
Outputlist1: [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
// 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);
}
}
Outputlist1: [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
// 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:
Similar Reads
Java Program to Find Average of Two Lists To calculate the average of two lists in Java we first need to combine the two lists into one. we can do this using the addAll() method of the ArrayList class. Once you have combined the lists we can calculate the average by summing up all the elements in the combined list and dividing by the total
2 min read
Program to Convert List to Stream in Java The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack class
3 min read
Java Program to Merge Two Arrays In Java, merging two arrays is a good programming question. We have given two arrays, and our task is to merge them, and after merging, we need to put the result back into another array. In this article, we are going to discuss different ways we can use to merge two arrays in Java.Let's now see the
5 min read
Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r
4 min read
How to Concatenate Multiple Strings in Java? In Java programming, there are a lot of ways to concatenate multiple strings. For this, we have taken three or more String values then we concatenate those String values by using different ways. In this article, we have used two different ways, we will explain each method with one example for a bett
2 min read