How to merge two lists in Java

In this shot, we will go over how to merge two lists in Java.

Lists are an important part of Java, and we widely use lists in different places. Merging two lists is an important concept, and there are various algorithms to perform this operation.

Solution approach

Here are the two most useful ways to perform the merge operation on lists.

  • The first solution is to use the addAll() method, which adds all the specified elements to the specified collection. We will create an empty list and then we will add all the elements of both the given lists to the new list with this method. The addAll() method takes the specified elements as parameters. We will pass both the given lists as a parameter one by one. Then the two lists will merge into a new list.

  • Another approach to merge two lists is to use the Java Stream. We can use stream to easily merge the lists. We will use three functions of stream:

    • stream(): Converts the given list into a stream.

    • concat(): Concatenates the streams; concat() takes two streams as parameters and returns the concatenated one.

    • collect(Collectors.toList()): Converts the stream into a list.

In this approach, we convert both lists into streams, then concatenate the stream and convert the concatenated stream into a list, and return the list.

Code

Let’s take a look at the code.

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class MergeList {
public static void main(String[] args) {
List<Integer> l1= new ArrayList<Integer>(){{
add(1);
add(2);
add(3);
}};
List<Integer> l2= new ArrayList<Integer>(){{
add(4);
add(5);
add(6);
add(7);
}};
List<Integer> ans= new ArrayList<Integer>(){{
addAll(l1);
addAll(l2);
}};
List<Integer> ans1= mergeUsingStream(l1,l2);
System.out.println("answer using Addall()\t"+ans);
System.out.println("answer using stream \t"+ans1);
}
static List<Integer> mergeUsingStream(List<Integer> l1, List<Integer> l2){
//convert both list into stream
Stream<Integer> list1= l1.stream();
Stream<Integer> list2= l2.stream();
// merge both the streams
Stream<Integer> list= Stream.concat(list1,list2);
//convert stream to list and return
return list.collect(Collectors.toList());
}
}

Explanation

  • In the first three lines of code, we import the libraries.
  • From lines 7 to 11, we initialize a list l1 of Integer type and add elements to it.
  • Similarly, from lines 12 to 17, we initialize l2 and add elements to it.
  • From lines 18 to 21, we create a new list ans and store the merged list we get from the first approach, i.e., using the addAll() method. Here, we use double brace initialization; this list also can be created by the traditional method.
  • In line 22, we call the function mergeUsingStream() and store the returned list in a variable ans1.
  • Then, we print the merged list that we get from both approaches.
  • From lines 19 to 26, we define the function mergeUsingStream(). In this function, we use the three functions described in the solution approach.

In this shot, we have seen how to merge two lists in Java by using the addAll() method and Java Stream.

Free Resources