Converting Between an Array and a Set in Java – 在Java中的数组和集合之间进行转换

最后修改: 2014年 5月 31日

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

1. Overview

1.概述

In this short article we’re going to look at converting between an array and a Set – first using plain java, then Guava and the Commons Collections library from Apache.

在这篇短文中,我们将看看arraySet之间的转换 – 首先使用普通java,然后是Guava和Apache的Commons Collections库。

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

本文是Baeldung网站上“Java – Back to Basic “系列的一部分。

2. Convert Array to a Set

2.将Array转换为Set

2.1. Using Plain Java

2.1.使用普通的Java

Let’s first look at how to turn the array to a Set using plain Java:

让我们首先看看如何使用普通的Java将数组转为Set

@Test
public void givenUsingCoreJavaV1_whenArrayConvertedToSet_thenCorrect() {
    Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
    Set<Integer> targetSet = new HashSet<Integer>(Arrays.asList(sourceArray));
}

Alternatively, the Set can be created first and then filled with the array elements:

或者,可以先创建Set,然后用数组元素填充。

@Test
public void givenUsingCoreJavaV2_whenArrayConvertedToSet_thenCorrect() {
    Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
    Set<Integer> targetSet = new HashSet<Integer>();
    Collections.addAll(targetSet, sourceArray);
}

2.2. Using Google Guava

2.2.使用Google Guava

Next, let’s look at the Guava conversion from array to Set:

接下来,让我们看看Guava从数组到Set的转换

@Test
public void givenUsingGuava_whenArrayConvertedToSet_thenCorrect() {
    Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
    Set<Integer> targetSet = Sets.newHashSet(sourceArray);
}

2.3. Using Apache Commons Collections

2.3.使用Apache Commons集合

Finally, let’s do the conversion using the Commons Collection library from Apache:

最后,让我们使用Apache的Commons Collection库来进行转换。

@Test
public void givenUsingCommonsCollections_whenArrayConvertedToSet_thenCorrect() {
    Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
    Set<Integer> targetSet = new HashSet<>(6);
    CollectionUtils.addAll(targetSet, sourceArray);
}

3. Convert Set to Array

3.将集合转换为数组

3.1. Using Plain Java

3.1.使用普通的Java

Now let’s look at the reverse – converting an existing Set to an array:

现在我们来看看相反的情况–将现有的Set转换为数组

@Test
public void givenUsingCoreJava_whenSetConvertedToArray_thenCorrect() {
    Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
    Integer[] targetArray = sourceSet.toArray(new Integer[0]);
}

Note, that toArray(new T[0]) is the preferred way to use the method over the toArray(new T[size]). As Aleksey Shipilëv proves in his blog post, it seems faster, safer, and cleaner.

注意,toArray(new T[0])是比toArray(new T[size])更好的使用方法。正如Aleksey Shipilëv在他的博客文章中所证明的那样,它似乎更快、更安全、更干净。

3.2. Using Guava

3.2.使用Guava

Next – the Guava solution:

下一步–番石榴方案。

@Test
public void givenUsingGuava_whenSetConvertedToArray_thenCorrect() {
    Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
    int[] targetArray = Ints.toArray(sourceSet);
}

Notice that we’re using the Ints API from Guava, so this solution is specific to the data type that we’re working with.

请注意,我们使用的是Guava的Ints API,所以这个解决方案是针对我们正在处理的数据类型的。

4. Conclusion

4.结论

The implementation of all 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的项目,所以应该很容易导入和运行。