Sometimes, we need to merge multiple lists into one before performing any operation, say Iteration or transformation. It's quite common to merge two lists, 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 lists 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 lists is by using addAll() method of java.util.Collection class, which allows you to add the content of one List into another List. By using the addAll() method you can add contents from as many List as you want, it's the best way to combine multiple List.
One thing to remember is that it also preserves the order on which objects from List are added, it actually appends at the end of the collection. So if you add List1 and then List2, the 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 a union of Set in Java, it provides a ListUtils class with a union method, which can be used to create a union of two List in Java. The result of the previous operation and is the same, It also preservers the order and appends elements of the second List after elements of the first List.
One thing to remember is that it also preserves the order on which objects from List are added, it actually appends at the end of the collection. So if you add List1 and then List2, the 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 a union of Set in Java, it provides a ListUtils class with a union method, which can be used to create a union of two List in Java. The result of the previous operation and is the same, It also preservers the order and appends elements of the second List after elements of the first List.
Java Code example to merge multiple List
Here is full code example of joining two List in Java. In this example, we have two List of Integers, one containing numbers in hundred series, while other contains numbers in thousands series. In order to merge these two, we create an ArrayList and used addAll() method to append objects form second List. In second way, we have used ListUtils.union() method to combine two Lists.
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.
what about margin 3 lists and more.
ReplyDelete