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()
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)
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
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
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
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!