1. Overview
In this tutorial, we will illustrate how to concatenate multiple collections into one logical collection.
We’ll be exploring five different approaches – two using Java 8, one using Guava, one using Apache Commons Collections, and one using only the standard Java 7 SDK.
In the examples that follow, let’s consider the following collections:
Collection<String> collectionA = asList("S", "T"); Collection<String> collectionB = asList("U", "V");
2. Using Java 8 Stream API
The Stream interface in Java API provides useful methods that make it easier to process collections. Let’s take a look at two of its methods – concat() and flatMap() – that are used for combining collections.
Once you obtain a Stream, you can perform aggregate operations on it.
2.1. Using the concat() Method
The static method concat() combines two Streams logically by creating a lazily concatenated Stream whose elements are all the elements of the first Stream followed by all the elements of the second Stream.
In the below example, let’s combine collectionA and collectionB using the concat() method:
Stream<String> combinedStream = Stream.concat( collectionA.stream(), collectionB.stream());
If you need to combine more than two Streams, you can invoke the concat() method again from within the original invocation:
Stream<String> combinedStream = Stream.concat( Stream.concat(collectionA.stream(), collectionB.stream()), collectionC.stream());
It is important to note that Java 8 Streams are not reusable, so you should take this into consideration when assigning them to variables.
2.2. Using the flatMap() Method
The flatMap() method returns a Stream after replacing each element of this Stream with the contents of a mapped Stream that is produced by applying the provided mapping function to each element.
The example below demonstrates merging of collections using the flatMap() method. Initially, you get a Stream whose elements are the two collections, and then you flatten the Stream before collecting it into a merged list:
Stream<String> combinedStream = Stream.of(collectionA, collectionB) .flatMap(Collection::stream); Collection<String> collectionCombined = combinedStream.collect(Collectors.toList());
3. Using Guava
The Guava library from Google provides several convenience methods for operating on collections and can be used with Java 6 or later.
3.1. Using the Iterables.concat() Method
The Iterables.concat() method is one of the Guava convenient methods that is used for merging collections:
Iterable<String> combinedIterables = Iterables.unmodifiableIterable( Iterables.concat(collectionA, collectionA));
The Iterable that is returned can be converted into a collection:
Collection<String> collectionCombined = Lists.newArrayList(combinedIterables);
3.2. Maven Dependency
Add the following dependency to your Maven pom.xml file to include the Guava library in your project:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>20.0</version> </dependency>
You can find the latest version of the Guava library in the Maven Central repository.
4. Using Apache Commons Collections
Apache Commons Collections is yet another library of utilities that assist in working with the various collections. The library provides two utility methods that can be used for combining collections. In this section, let us understand how these methods work.
4.1. Using the IterableUtils.chainedIterable() Method
The IterableUtils class provides utility methods and decorators for Iterable instances. It provides the the chainedIterable() method, which can be used to combine multiple Iterables into a single one.
Iterable<String> combinedIterables = IterableUtils.chainedIterable( collectionA, collectionB);
4.2. Using the CollectionUtils.union() Method
Utility methods and decorators for Collection instances are provided by the CollectionUtils class. The union() method from this class returns a Collection containing the union of the given Iterable instances.
Iterable<String> combinedIterables = CollectionUtils.union( collectionA, collectionB);
In the case of the union() method, the cardinality of each element in the returned collection will be equal to the maximum of the cardinality of that element in the two given Iterables. This means that the combined collection only consists of the elements in the first collection and the elements in the second collection that were not present in the first one.
4.3. Maven Dependency
Add the following dependency to your Maven pom.xml file to include the Apache Commons Collections library in your project:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency>
You can find the latest version of the Apache Commons library in the Maven Central repository.
5. Using Java 7
If you are still using Java 7 and wish to avoid third party libraries such as Guava, you can use the addAll() method to combine elements from multiple collections, or you can write your own utility methods to combine Iterables.
5.1. Using the addAll() Method
Of course the simplest solution for combining collections is using the addAll() method, as in the following List example, however it is worth noting that this method creates a new collection with additional references to the same objects that are in the first two collections:
List<String> listC = new ArrayList<>(); listC.addAll(listA); listC.addAll(listB);
5.2. Writing a Custom concat() Method
The below example defines a concat() method that accepts two Iterables and returns a merged Iterable object:
public static <E> Iterable<E> concat( Iterable<? extends E> i1, Iterable<? extends E> i2) { return new Iterable<E>() { public Iterator<E> iterator() { return new Iterator<E>() { Iterator<? extends E> listIterator = i1.iterator(); Boolean checkedHasNext; E nextValue; private boolean startTheSecond; void theNext() { if (listIterator.hasNext()) { checkedHasNext = true; nextValue = listIterator.next(); } else if (startTheSecond) checkedHasNext = false; else { startTheSecond = true; listIterator = i2.iterator(); theNext(); } } public boolean hasNext() { if (checkedHasNext == null) theNext(); return checkedHasNext; } public E next() { if (!hasNext()) throw new NoSuchElementException(); checkedHasNext = null; return nextValue; } public void remove() { listIterator.remove(); } }; } }; }
The concat() method can be invoked by passing the two collections as its arguments:
Iterable<String> combinedIterables = concat(collectionA, collectionB); Collection<String> collectionCombined = makeListFromIterable(combinedIterables);
If you need the Iterable to be available as a List, you can also use the makeListFromIterable() method that creates a List using the members of the Iterable:
public static <E> List<E> makeListFromIterable(Iterable<E> iter) { List<E> list = new ArrayList<E>(); for (E item : iter) { list.add(item); } return list; }
6. Conclusion
The article discussed several different ways to combine two collections logically in Java without creating additional references to the objects they contain.
The code for this tutorial is available over on Github.