Java – Combine Multiple Collections – Java – 合并多个集合

最后修改: 2016年 12月 18日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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.

我们将探索五种不同的方法–两种使用Java 8,一种使用Guava,一种使用Apache Commons Collections,还有一种只使用标准的Java 7 SDK。

In the examples that follow, let’s consider the following collections:

在下面的例子中,让我们考虑以下集合。

Collection<String> collectionA = Arrays.asList("S", "T");
Collection<String> collectionB = Arrays.asList("U", "V");

2. Using Java 8 Stream API

2.使用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.

Java API中的Stream接口提供了有用的方法,使处理集合更加容易。让我们来看看其中的两个方法–concat()flatMap()–用于组合集合。

Once you obtain a Stream, you can perform aggregate operations on it.

一旦你获得一个Stream,你就可以对它进行聚合操作。

2.1. Using the concat() Method

2.1.使用concat()方法

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.

静态方法concat()通过创建一个懒惰地连接的Stream来逻辑地结合两个Stream,其元素是第一个Stream的所有元素和第二个Stream的所有元素。

In the below example, let’s combine collectionA and collectionB using the concat() method:

在下面的例子中,让我们使用concat()方法将collectionAcollectionB结合起来。

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:

如果你需要结合两个以上的Streams,你可以在原调用中再次调用concat()方法。

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.

需要注意的是,Java 8的Streams是不可重用的,所以你在将它们分配给变量时应该考虑到这一点。

2.2. Using the flatMap() Method

2.2.使用flatMap() 方法

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.

flatMap()方法在将此Stream的每个元素替换为映射的Stream的内容后返回Stream,该内容是通过对每个元素应用提供的映射函数产生的。

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:

下面的例子演示了使用flatMap()方法对集合进行合并。最初,你得到一个Stream,其元素是两个集合,然后你在将其收集成一个合并的列表之前将Stream压平。

Stream<String> combinedStream = Stream.of(collectionA, collectionB)
  .flatMap(Collection::stream);
Collection<String> collectionCombined = 
  combinedStream.collect(Collectors.toList());

3. Using Guava

3.使用番石榴

The Guava library from Google provides several convenience methods for operating on collections and can be used with Java 6 or later.

来自Google的Guava库提供了几种操作集合的便利方法,可以在Java 6或更高版本中使用。

3.1. Using the Iterables.concat() Method

3.1.使用Iterables.concat()方法

The Iterables.concat() method is one of the Guava convenient methods that is used for merging collections:

Iterables.concat()方法是Guava方便的方法之一,用于合并集合。

Iterable<String> combinedIterables = Iterables.unmodifiableIterable(
  Iterables.concat(collectionA, collectionA));

The Iterable that is returned can be converted into a collection:

返回的Iterable可以被转换为一个集合。

Collection<String> collectionCombined = Lists.newArrayList(combinedIterables);

3.2. Maven Dependency

3.2.Maven的依赖性

Add the following dependency to your Maven pom.xml file to include the Guava library in your project:

在Maven的pom.xml文件中添加以下依赖关系,以便在项目中包含Guava库。

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

You can find the latest version of the Guava library in the Maven Central repository.

您可以在Maven Central资源库中找到Guava库的最新版本。

4. Using Apache Commons Collections

4.使用Apache Commons集合

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.

Apache Commons Collections是另一个协助处理各种集合的实用程序库。该库提供了两种实用的方法,可用于组合集合。在本节中,让我们了解这些方法是如何工作的。

4.1. Using the IterableUtils.chainedIterable() Method

4.1.使用IterableUtils.chinedIterable()方法

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.

IterableUtils类为Iterable实例提供实用方法和装饰器。它提供了chainedIterable()方法,该方法可用于将多个Iterable合并为一个。

Iterable<String> combinedIterables = IterableUtils.chainedIterable(
  collectionA, collectionB);

4.2. Using the CollectionUtils.union() Method

4.2.使用CollectionUtils.union()方法

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.

Collection实例的实用方法和装饰器由CollectionUtils类提供。这个类的union()方法返回一个Collection,其中包含给定的Iterable实例的联合。

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.

union()方法的情况下,返回的集合中每个元素的cardinality将等于该元素在两个给定Iterables中的cardinality的最大值。这意味着合并后的集合只由第一个集合中的元素和第二个集合中不存在的元素组成。

4.3. Maven Dependency

4.3.Maven的依赖性

Add the following dependency to your Maven pom.xml file to include the Apache Commons Collections library in your project:

在你的Maven pom.xml文件中添加以下依赖,以便在项目中加入Apache Commons集合库。

<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.

您可以在Maven Central资源库中找到最新版本的Apache Commons库。

5. Using Java 7

5.使用Java 7</strong

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.

如果你还在使用Java 7,并希望避免使用第三方库,如Guava,你可以使用addAll()方法来组合多个集合中的元素,或者你可以编写自己的实用方法来组合Iterables

5.1. Using the addAll() Method

5.1.使用addAll()方法

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:

当然,合并集合的最简单的解决方案是使用addAll()方法,就像下面的List例子一样,但是值得注意的是,这个方法会创建一个新的集合,其中有对前两个集合中相同对象的额外引用。

List<String> listC = new ArrayList<>();
listC.addAll(listA);
listC.addAll(listB);

5.2. Writing a Custom concat() Method

5.2.编写一个自定义的concat()方法

The below example defines a concat() method that accepts two Iterables and returns a merged Iterable object:

下面的例子定义了一个concat()方法,它接受两个Iterables并返回一个合并的Iterable对象。

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:

concat()方法可以通过传递两个集合作为其参数来调用。

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:

如果您需要Iterable作为List可用,您也可以使用makeListFromIterable()方法,该方法使用Iterable的成员创建List

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

6.结论

The article discussed several different ways to combine two collections logically in Java without creating additional references to the objects they contain.

这篇文章讨论了在Java中逻辑地结合两个集合的几种不同方法,而不需要对它们所包含的对象创建额外的引用。

The code for this tutorial is available over on Github.

本教程的代码可在Github上获得