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
Using Java 8 Streams there are two basic ways of merging collections together. 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 together.
Before we do that, next are the two collections that we want to merge together.
Collection<Integer> collection1 = List.of(1, 2, 3); Collection<Integer> collection2 = List.of(97, 98, 99);
Next, is how we can use flatMap
to merge these two collections.
Collection<Integer> merged = Stream.of(collection1, collection2) .flatMap(Collection::stream) .collect(Collectors.toList());
First, we are creating a stream of two collections. That means, the stream will have only two elements and those are the two collections. After which, we are using flatMap
to merge them together to create a combined steam of their elements. Finally, we are collecting the stream elements together in anArrayList
.
The output we get, look 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 elements combined.
Collection<Integer> merged = Stream .concat(collection1.stream(), collection2.stream()) .collect(Collectors.toList());
Here, we are creating individual streams from both of our collections and using 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 Immutable List that we used in 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);
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
Till now, we have seen Java’s own ways of merging or concatenating collections together. Now, we will se how can we use 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. Doing so, the method dumps 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);
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 modified or not.
Concatenate Iterables using concat Method
The Iterables class provides concat
method which 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);
Note that, this method creates a new instance hence we an pass Immutable Lists.
Concatenate Collections using Apache Commons Collections
Similarly, we can use Apache Commons Collections Library to merge collections together.
To do this, 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 together 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);
However, there are two interesting facts about this method.
- The union method first copies all the elements from first collection, and from second collection it only takes those elements which are not present in the first.
- Unlike other ways, which concatenates elements of second collection after the end of the first, the union method actually merges elements together. To understand this, we will print the merged 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 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);
Unlike the union method of CollectionUtils, this method simply concatenates elements of the second iterable at the end of the first one.
Concatenate Collections without Duplicates
Using Java Streams API we can concatenate multiple collections together and form a new collection of unique elements. To know more ways of removing duplicates from List consider reading our detailed tutorial Removing Duplicate Elements from Java List.
We have already seen an example of 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());
The output we get is
[1, 2, 3, 97, 98, 99]
Summary
In this Tutorial of Merging or Concatenating Java Collections together we covered various examples. We have seen a plenty of examples using Java 8, using Plain java, using Guava Library, and using Apache Commons Collections Library. Finally we saw Java 8 way of concatenating collections together without duplicate elements.