Home > Java > javaTutorial > body text

Interpretation of Java documentation: Detailed explanation of the usage of toArray() method of HashSet class

王林
Release: 2023-11-04 13:40:59
Original
874 people have browsed it

Interpretation of Java documentation: Detailed explanation of the usage of toArray() method of HashSet class

HashSet is a very commonly used collection class provided in Java, which can store a set of non-repeating, unordered elements. When using HashSet, sometimes you need to convert the elements in the HashSet into an array. In this case, you need to use the toArray() method it provides. Below we will explain in detail the usage of the toArray() method of the HashSet class, and also provide some specific code examples.

First, let's take a look at the definition of the toArray() method:

public Object[] toArray()
Copy after login

This method returns an array containing the elements in the HashSet object. The order of the elements in the array is unordered. The returned array type is Object[], so it needs to be cast to other types of arrays. If you want to convert a HashSet into an array of a specified type, you need to use another toArray() method with parameters, which is defined as follows:

public <T> T[] toArray(T[] a)
Copy after login

This method accepts a parameter a, which is what we want to convert to target array. If the length of array a is less than the size of HashSet, return a new array whose length is equal to the size of HashSet and whose type is the same as array a. If the length of array a is greater than the size of HashSet, the elements in HashSet are copied to array a, and the remaining elements are set to null.

Below, we show the usage of toArray() with specific code examples.

1. Using the default return type

HashSet<String> hashSet = new HashSet<>();
hashSet.add("apple");
hashSet.add("banana");
hashSet.add("orange");
Object[] array = hashSet.toArray();
for (Object obj : array) {
    System.out.print(obj + " ");
}
//输出结果:banana apple orange 
Copy after login

you can see that even if the order we add is "apple", "banana", "orange", the actual output result is "banana" , "apple", "orange", because HashSet is unordered.

2. Convert HashSet into a string array

HashSet<String> hashSet = new HashSet<>();
hashSet.add("apple");
hashSet.add("banana");
hashSet.add("orange");
String[] array = hashSet.toArray(new String[hashSet.size()]);
for (String str : array) {
    System.out.print(str + " ");
}
//输出结果:banana apple orange 
Copy after login

When using this method, you need to specify the type of the target array. We use a new String type array here, and the array length is HashSet. size so it can accommodate all elements. The toArray() method returns an Object[] type array, which needs to be cast to a String[] type array.

3. Convert HashSet to integer array

HashSet<Integer> hashSet = new HashSet<>();
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
Integer[] array = hashSet.toArray(new Integer[hashSet.size()]);
for (Integer num : array) {
    System.out.print(num + " ");
}
//输出结果:1 2 3 
Copy after login

Similarly, if we need to convert HashSet to integer array, we need to use Integer[] array as the target array type, and we need to The array length is specified as the size of the HashSet.

Through the above three examples, we can see that the toArray() method is very flexible and convenient to use in HashSet, and its usage is also very simple and clear. You can use it according to your own needs.

The above is the detailed content of Interpretation of Java documentation: Detailed explanation of the usage of toArray() method of HashSet class. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!