Sometime, we need to merge multiple list into one before performing any
operation, say Iteration or transformation. It's quite common to merge two
list, or combine them into a bigger list and there are multiple ways to do it.
In this article, we will take a look at two simple way to join two list in Java, you can further extend that idea to join any
number of List or it's implementation e.g. ArrayList
or LinkedList in Java. One way to merge multiple list is by using addAll() method of java.util.Collection class,
which allows you to add content of one List into
another List. By using addAll() method you
can add contents from as many List as you want, it's best way to combine
multiple List. One thing to remember is that, it also preservers the order on
which objects from List are added, it actually appends at the end of
collection. So if you add List1 and than List2, content
of List1 will come before elements of List2. You can
even use this technique to combine multiple List into a Set to remove any
duplicates. Another way of merging ArrayList is using
Apache Commons Collection, Apart from several goodies, like creating
union of Set in Java, it provides a ListUtils class with
a union method, which can be used to create union of two List in Java. Result
of previous operation and this is same, It also preservers the order and
appends elements of second List after elements of first List.
Java Code example to merge multiple List

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections.ListUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Java program to merge multiple List into a
single List using JDK and open source
* Apache Commons library.
*
* @author Javin Paul
*/
public class MergeList
{
private static
final Logger logger = LoggerFactory.getLogger(MergeList.class);
public static
void main(String
args[]) {
List hundreads = Arrays.asList(101,102,103);
List thousands = Arrays.asList(1001,1002,1003);
// merging two list using core Java
List merged = new
ArrayList(hundreads);
merged.addAll(thousands);
System.out.println("List 1 : " +
hundreads);
System.out.println("List 2 : " +
thousands);
System.out.println("Merged List : " +
merged);
// another
way to merge two list in Java
// using ListUtils from Apache commons Collection
merged
= ListUtils.union(hundreads, thousands);
System.out.println("Merged List using Apache Commons
Collections: " + merged);
}
}
Output:
List 1 : [101, 102,
103]
List 2 : [1001, 1002,
1003]
Merged List : [101,
102, 103,
1001, 1002,
1003]
Merged List using Apache Commons Collections: [101,
102, 103,
1001, 1002,
1003]
That's all on this tutorial about merging two List in Java using
JDK and Apache Commons Collections library. It's a very useful trick and can be
useful in several cases. You can even extend this idea to create a Set of unique
elements from multiple List as well, addAll() method
from Collection, accepts any Collection
implementation including ArrayList, LinkedList etc.
No comments:
Post a Comment