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. TheaddAll()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 usestreamto easily merge the lists. We will use three functions ofstream:-
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 streamStream<Integer> list1= l1.stream();Stream<Integer> list2= l2.stream();// merge both the streamsStream<Integer> list= Stream.concat(list1,list2);//convert stream to list and returnreturn 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
l1ofIntegertype and add elements to it. - Similarly, from lines 12 to 17, we initialize
l2and add elements to it. - From lines 18 to 21, we create a new list
ansand store the merged list we get from the first approach, i.e., using theaddAll()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 variableans1. - 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.