HashSet toArray() Method in Java – Java 中的 HashSet toArray() 方法

最后修改: 2023年 11月 13日

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

1. Introduction

1.导言

HashSet is one of the common data structures that we can utilize in Java Collеctions.

HashSet 是我们可以在 Java Collеctions 中使用的常用数据结构之一。

In this tutorial, we’ll dive into the toArray() method of the HashSet class, illustrating how to convert a HashSet to an array.

在本教程中,我们将深入研究 HashSet 类的toArray()方法,说明如何将 HashSet 转换为数组。

2. Convеrting HashSеt to Array

2.将 HashSеt 转换为 Array</em

Let’s look at a set of examples that illustrate how to apply the toArray() method to convert a HashSet into an array.

下面我们来看一组示例,说明如何应用 toArray() 方法将 HashSet 转换为数组。

2.1. HashSet to an Array of Strings

2.1.将 HashSet 转换为 StringsArray</em

In the following method, we are seeking to convert a HashSet of strings into an array of strings:

在下面的方法中,我们试图将字符串 HashSet 转换为字符串数组:

@Test
public void givenStringHashSet_whenConvertedToArray_thenArrayContainsStringElements() {
    HashSet<String> stringSet = new HashSet<>();
    stringSet.add("Apple");
    stringSet.add("Banana");
    stringSet.add("Cherry");

    // Convert the HashSet of Strings to an array of Strings
    String[] stringArray = stringSet.toArray(new String[0]);

    // Test that the array is of the correct length
    assertEquals(3, stringArray.length);

    for (String str : stringArray) {
        assertTrue(stringSet.contains(str));
    }
}

Here, a HashSet named stringSet is initialized with three String elements: (“Apple” “Banana” and “Cherry“). To be specific, the test method ensures that the resulting array has a length of 3, matching the number of elements in the HashSet.

在这里,一个名为 stringSetHashSet 被初始化为三个 String 元素:(“Apple” “Banana” 和 “Cherry“)。具体来说,测试方法将确保生成的数组长度为 3,与 HashSet 中元素的数量相匹配。

Then, it iterates through the stringArray and checks if each element is contained within the original stringSet, asserting that the array indeed contains the String elements, confirming the successful conversion of the HashSet to a String array. 

然后,它会遍历 stringArray 并检查每个元素是否包含在原始 stringSet 中,断言数组确实包含 String 元素,从而确认 HashSet 已成功转换为字符串数组。

2.2. HashSet to an Array of Integers

2.2.将 HashSet 转换为 IntegersArray</em

Additionally, we can utilize the toArray() method to convert an Integer HashSet into an array of Integers as follows:

此外,我们还可以使用 toArray() 方法将 Integer HashSet 转换为由 Integer 组成的数组,如下所示:

@Test
public void givenIntegerHashSet_whenConvertedToArray_thenArrayContainsIntegerElements() {
    HashSet<Integer> integerSet = new HashSet<>();
    integerSet.add(5);
    integerSet.add(10);
    integerSet.add(15);

    // Convert the HashSet of Integers to an array of Integers
    Integer[] integerArray = integerSet.toArray(new Integer[0]);

    // Test that the array is of the correct length
    assertEquals(3, integerArray.length);

    for (Integer num : integerArray) {
        assertTrue(integerSet.contains(num));
    }

    assertTrue(integerSet.contains(5));
    assertTrue(integerSet.contains(10));
    assertTrue(integerSet.contains(15));
}

Here, we create a HashSet named integerSet with three Integer elements: (5, 10, and 15). The test method is responsible for verifying the conversion of this Integer HashSet into an array of Integers, referred to as integerArray.

在此,我们创建一个名为 integerSetHashSet 并包含三个整数元素:(5、10 和 15)。测试方法负责验证将此 Integer HashSet 转换为 Integer 数组(称为 integerArray )的过程。

Moreover, it confirms that the resulting array has length = 3, corresponding to the number of elements in the original HashSet. Subsequently, the method iterates through integerArray, ensuring each element is contained within the original integerSet.

此外,它还确认生成的数组具有长度 = 3,这与原始 HashSet 中的元素数目相对应。随后,该方法将遍历 integerArray ,确保每个元素都包含在原始 integerSet 中。

3. Conclusion

3.结论

In conclusion, it is easy to convert a HashSet into an array using the toArray() method of the HashSet class. This can also be useful while handling array-based data structures or some other components in our Java apps.

总之,使用 HashSet 类的 toArray() 方法可以轻松地将 HashSet 转换为数组。这在我们的 Java 应用程序中处理基于数组的数据结构或其他组件时也很有用。

As always, the complete code samples for this article can be found over on GitHub.

与往常一样,本文的完整代码示例可在 GitHub 上找到