Converting Between a List and a Set in Java – 在Java中的列表和集合之间进行转换

最后修改: 2014年 5月 31日

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

1. Overview

1.概述

In this quick tutorial, we’ll take a look at the conversion between a List and a Set, starting with Plain Java, using Guava and the Apache Commons Collections library, and finally with Java 10.

在这个快速教程中,我们将看看ListSet之间的转换,从Plain Java开始,使用Guava和Apache Commons Collections库,最后使用Java 10。

This article is part of the “Java – Back to Basic” series here on Baeldung.

这篇文章是Baeldung网站上“Java–回到基础 “系列的一部分。

2. Convert List to Set

2.将List转换为Set

2.1. With Plain Java

2.1.使用普通的Java

Let’s start with converting a List to a Set using Java:

让我们从使用Java将一个List转换为Set开始

public void givenUsingCoreJava_whenListConvertedToSet_thenCorrect() {
    List<Integer> sourceList = Arrays.asList(0, 1, 2, 3, 4, 5);
    Set<Integer> targetSet = new HashSet<>(sourceList);
}

As we can see, the conversion process is type-safe and straightforward, since the constructors of each collection do accept another collection as a source.

正如我们所看到的,转换过程是类型安全和直接的,因为每个集合的构造函数确实接受另一个集合作为源。

2.2. With Guava

2.2.使用Guava

Let’s do the same conversion using Guava:

让我们用Guava做同样的转换。

public void givenUsingGuava_whenListConvertedToSet_thenCorrect() {
    List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
    Set<Integer> targetSet = Sets.newHashSet(sourceList);
}

2.3. With Apache Commons Collections

2.3.使用Apache Commons Collections

Next let’s use the Commons Collections API to convert between a List and a Set:

接下来让我们使用Commons Collections API在ListSet之间转换。

public void givenUsingCommonsCollections_whenListConvertedToSet_thenCorrect() {
    List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
    Set<Integer> targetSet = new HashSet<>(6);
    CollectionUtils.addAll(targetSet, sourceList);
}

2.4. With Java 10

2.4.有了Java 10

One additional option is to use the Set.copyOf static factory method introduced in Java 10:

一个额外的选择是使用Java 10中引入的Set.copyOf静态工厂方法。

public void givenUsingJava10_whenListConvertedToSet_thenCorrect() {
    List sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
    Set targetSet = Set.copyOf(sourceList);
}

Note that a Set created this way is unmodifiable.

注意,以这种方式创建的Set不可修改的

3. Convert Set to List

3.将Set转换成List

3.1. With Plain Java

3.1.使用普通的Java

Now let’s do the reverse conversion, from a Set to a List, using Java:

现在让我们做一下反向转换,SetList,使用Java

public void givenUsingCoreJava_whenSetConvertedToList_thenCorrect() {
   Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
   List<Integer> targetList = new ArrayList<>(sourceSet);
}

3.2. With Guava

3.2.与瓜娃

We can do the same using the Guava solution:

我们可以用Guava的解决方案做同样的事情。

public void givenUsingGuava_whenSetConvertedToList_thenCorrect() {
    Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
    List<Integer> targetList = Lists.newArrayList(sourceSet);
}

This is very similar to the java approach, only with a little less duplicated code.

这与java的方法非常相似,只是重复的代码少了一点。

3.3. With Apache Commons Collections

3.3.使用Apache Commons Collections

Now let’s see the Commons Collections solution to convert between a Set and a List:

现在让我们看看Commons Collections的解决方案,在SetList之间进行转换。

public void givenUsingCommonsCollections_whenSetConvertedToList_thenCorrect() {
    Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
    List<Integer> targetList = new ArrayList<>(6);
    CollectionUtils.addAll(targetList, sourceSet);
}

3.4. With Java 10

3.4.有了Java 10

Finally, we can use the List.copyOf that’s been introduced in Java 10:

最后,我们可以使用Java 10中引入的List.copyOf

public void givenUsingJava10_whenSetConvertedToList_thenCorrect() {
    Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
    List<Integer> targetList = List.copyOf(sourceSet);
}

We need to keep in mind that the resulting List is unmodifiable.

我们需要记住,产生的List不可修改的

4. Conclusion

4.结论

The implementation of all of these examples and code snippets can be found over on GitHub. This is a Maven-based project, so it should be easy to import and run as it is.

所有这些例子和代码片段的实现都可以在GitHub上找到。这是一个基于Maven的项目,所以应该很容易导入并按原样运行。