Combine or Merge Multiple Lists in Java
Combing or merging lists is very common and frequent operation, these lists could be ArrayLists or LinkedLists. In this article, we are going to discuss for joining or concatenating or merging two or more lists into single list.
OPTION 1: Before Java 8
Collection.addAll()
is the most obvious option to combine multiple lists into single list, here is the sample codes:
private static void addAll() {
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(4, 5, 6);
List<Integer> list3 = Arrays.asList(7, 8, 9);
List<Integer> mergedList = new ArrayList<>();
mergedList.addAll(list1);
mergedList.addAll(list2);
mergedList.addAll(list3);
log.info("merged list: {}", mergedList);
}
OPTION 2: Using Java 8 Stream API
If you have only 2 input lists, you can also use Stream.concat()
.
private static void streamConcat() {
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(4, 5, 6);
List<Integer> mergedList = Stream.concat(list1.stream(), list2.stream()).collect(Collectors.toList());
log.info("merged list: {}", mergedList);
}
Stream.concat()
only accept 2 arguments, If you have more than 2 input lists, you can use Stream.of()
together with Stream.flatMap()
instead.
private static void streamFlatMap() {
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(4, 5, 6);
List<Integer> list3 = Arrays.asList(7, 8, 9);
List<Integer> mergedList = Stream.of(list1, list2, list3).flatMap(Collection::stream).collect(Collectors.toList());
log.info("merged list: {}", mergedList);
}
OPTION 3: Using Apache Common Collections
If you have Apache common collections in the project classpath, ListUtils.union()
is another option to merge 2 lists.
private static void apacheCommonUnion() {
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(4, 5, 6);
List<Integer> mergedList = ListUtils.union(list1, list2);
log.info("merged list: {}", mergedList);
}
IterableUtils.chainedIterable()
can also be used to merge lists. It accepts lists as arguments and returns an Iteratble object, which can be converted as list by IterableUtils.toList()
.
private static void apacheCommonChainedIterable() {
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(4, 5, 6);
List<Integer> list3 = Arrays.asList(7, 8, 9);
Iterable<Integer> iterable = IterableUtils.chainedIterable(list1, list2, list3);
List<Integer> mergedList = IterableUtils.toList(iterable);
log.info("merged list: {}", mergedList);
}
OPTION 4: Using Google Guava
If you have Google Guava in your project, you can also use Iterables.concat()
, the method combines multiple iterables into a single iterable.
private static void googleGuavaIterablesConcat() {
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(4, 5, 6);
List<Integer> list3 = Arrays.asList(7, 8, 9);
List<Integer> list4 = Arrays.asList(10, 11, 12);
List<Integer> list5 = Arrays.asList(13, 14, 15);
List<Integer> list6 = Arrays.asList(16, 17, 18);
Iterable<Integer> iterable = Iterables.concat(list1, list2, list3, list4, list5, list6);
List<Integer> mergedList = Lists.newArrayList(iterable);
log.info("merged list: {}", mergedList);
}
There is a Stream
equivalent of the above method for java 8 users: Streams.concat()
private static void googleGuavaStreamsConcat() {
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(4, 5, 6);
List<Integer> list3 = Arrays.asList(7, 8, 9);
List<Integer> list4 = Arrays.asList(10, 11, 12);
List<Integer> mergedList = Streams.concat(
list1.stream(), list2.stream(), list3.stream(), list4.stream())
.collect(Collectors.toList());
log.info("merged list: {}", mergedList);
}