Learn different techniques to Merge or Concatenate multiple collections together in Java with the help of practical examples.
Tutorial Contents
Overview
There are different ways available to merge or concatenate multiple Java Collections together. Java Collection is the parent interface of all collections available in Java.
Concatenate Collections using Java Streams
With Java 8 Streams, there are two basic ways of merging collections. Next, we will look at both of them one by one.
Concatenate Using Java Streams flatMap Method
We will use flatMap
method of Java Streams to merge two collections.
Before we do that, the following are the two collections that we want to join together.
Collection<Integer> collection1 = List.of(1, 2, 3);
Collection<Integer> collection2 = List.of(97, 98, 99);
Code language: Java (java)
Next is how we can use flatMap to combine these two collections.
Collection<Integer> merged = Stream.of(collection1, collection2)
.flatMap(Collection::stream)
.collect(Collectors.toList());
Code language: Java (java)
First, we are creating a stream of two collections. That means the stream will have only two elements, which are the two collections. After which, we use flatMap to merge them to create combined steam of their elements. Finally, we are collecting the stream elements together in anArrayList
.
The output we get looks like this:
[1, 2, 3, 97, 98, 99]
Concatenate using Java Streams Concat Method
The concat
method of Streams interface merges elements from multiple streams to form a stream of combined elements.
Collection<Integer> merged = Stream
.concat(collection1.stream(), collection2.stream())
.collect(Collectors.toList());
Code language: Java (java)
Here, we are creating individual streams from both of our collections and using the concat
method to form a single stream of their elements combined. Lastly, we are collecting the elements together in an ArrayList.
Concatenate Collections in Plain Java
The Java Collections interface defines addAll
method. We can call this method on one collection and pass another collection as an argument.
However, with this method, this collection will be modified. Hence we can’t use the Immutable List that we used in the previous examples.
Collection<Integer> collection1 = new ArrayList<>(Arrays.asList(1, 2, 3));
Collection<Integer> collection2 = new ArrayList<>(Arrays.asList(97, 98, 99));
boolean hasModified = collection1.addAll(collection2);
Code language: Java (java)
We have created two ArrayList instances and executed addAll
on one of them. The boolean result denotes if the merge was successful.
Concatenate Collections using Guava
We have seen Java’s ways of merging or concatenating collections together. Now, we will see how can we use the Guava library to do so.
Before that, make sure you have Guava dependency in your build.
Concatenate using Iterables addAll method
The Iterables class has addAll
method which accepts two collection arguments. In doing so, the method dumps the second collection into the first one.
Collection<Integer> collection1 = new ArrayList<>(Arrays.asList(1, 2, 3));
Collection<Integer> collection2 = List.of(2, 97, 98, 99);
boolean hasModified = Iterables.addAll(collection1, collection2);
Code language: Java (java)
Note that, with this method, the first collection is modified. That is why our first collection is not Immutable. The resulting boolean value denotes if the first collection is altered or not.
Concatenate Iterables using concat Method
The Iterables class provides concat
method that accepts n number of Iterable instances and returns a new Iterable instance having all elements concatenated.
Collection<Integer> collection1 = List.of(1, 2, 3);
Collection<Integer> collection2 = List.of(2, 97, 98, 99);
Iterable<Integer> merged = Iterables.concat(collection1, collection2);
Code language: Java (java)
Note that this method creates a new instance; hence we can pass Immutable Lists.
Concatenate Collections using Apache Commons Collections
Similarly, we can use Apache Commons Collections Library to merge collections.
We need to add Apache Commons Collections dependencies in your build.
Merge Collections using CollectionUtils
The union methods from the CollectionUtils
class can merge two collections to form a third one.
Collection<Integer> collection1 = List.of(1, 1, 2, 3, 4);
Collection<Integer> collection2 = List.of(2, 97, 98, 99, 100);
Collection<Integer> merged = CollectionUtils
.union(collection1, collection2);
Code language: Java (java)
However, there are two interesting facts about this method.
- The union method first copies all the elements from the first collection. After that, it only picks those elements from the second collection which are not part of the first.
- Unlike other ways, which concatenate elements of the second collection after the end of the first, the union method merges elements. To understand this, we will print the combined collection.
[1, 1, 97, 2, 98, 3, 99, 4, 100]
The output shows how the elements from both collections are merged together, and also, the number 2 from the second List is not copied.
Concatenate Iterables using IterableUtils
The IterableUtils
class from Apache Commons Library provides a way to concatenate two iterables together.
Collection<Integer> collection1 = List.of(1, 1, 2, 3, 4);
Collection<Integer> collection2 = List.of(2, 97, 98, 99, 100);
Iterable<Integer> merged = IterableUtils
.chainedIterable(collection1, collection2);
Code language: Java (java)
Unlike the union
method of CollectionUtils, this method concatenates elements of the second iterable at the end of the first one.
Concatenate Collections without Duplicates
Using Java Streams API, we can combine multiple collections and form a new collection of unique elements. To know more ways of removing duplicates from a List, consider reading our detailed tutorial Removing Duplicate Elements from Java List.
We have already seen an example of the Java Streams Concat Method. We will add distinct to the stream pipeline to filter out duplicate elements.
Collection<Integer> collection1 = List.of(1, 2, 3, 3);
Collection<Integer> collection2 = List.of(2, 97, 98, 99);
Iterable<Integer> merged = Streams
.concat(collection1.stream(), collection2.stream())
.distinct()
.collect(Collectors.toList());
Code language: Java (java)
The output we get is
[1, 2, 3, 97, 98, 99]
Summary
In this tutorial, we thoroughly understood how to merge multiple Java Collections to form a collection of combined elements. We covered plenty of examples using Java Streams, plain Java, Guava library, and Apache Commons Collections library. Lastly, we saw Java 8 streams way of concatenating collections without duplicate elements.