In Java, toArray() copies the elements of a HashSet into a new array, in the same order the iterator would return them. The array is independent of the set: later changes to either one do not affect the other.
There are two forms. toArray() returns an Object[]. toArray(T[] a) returns an array of the type you pass, which is what you want for String[] or Integer[].
An instance method of java.util.HashSet (class since Java 1.2): a set of unique elements kept in a hash table, with no guaranteed order, one null element allowed, and no thread safety.
Syntax
Without arguments you get Object[]; with an array argument you get your own type.
Elements need a cast. Each item of the Object[] is cast to Integer before it is added to the total of 70 + 85 + 90 = 245.
The array itself cannot be cast. The no-argument form builds an array whose real type is Object[], and Java refuses to treat it as Integer[] at run time.
The compiler cannot see it. The cast is legal syntax, so the failure only shows up when the line executes, inside the try block.
The typed form works. Passing new Integer[0] makes toArray create a real Integer[] of length 3.
Example 3 — A Bigger Array Gets a null Marker
Pass an array with spare room, then one that is too small, and see which array comes back.
Java
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class HashSetToArrayBigger {
public static void main(String[] args) {
Set<String> letters = new HashSet<>();
letters.add("x");
letters.add("y");
String[] slots = new String[4];
Arrays.fill(slots, "-");
String[] result = letters.toArray(slots);
System.out.println("same array: " + (result == slots));
System.out.println("slots: " + Arrays.toString(slots));
String[] small = new String[1];
String[] other = letters.toArray(small);
System.out.println("small reused: " + (other == small) + ", length " + other.length);
}
}
Output
same array: true
slots: [x, y, null, -]
small reused: false, length 2
HashSet iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
Room to spare.slots has four places for two letters, so toArray fills the array you passed and returns that same object.
The marker. The first two places hold the elements, the third (right after the last element) is set to null, and the fourth keeps its old "-".
Too small.small has one place for two letters, so toArray allocates a new String[] of length 2 instead.
Use the return value. Always read the result rather than the array you passed, because it may be a different array.
Frequently Asked Questions
Call set.toArray(new String[0]) for a String[] (use the right type for your elements), or set.toArray() for an Object[].
The no-argument toArray() creates an Object[]. To get a String[], pass an array: set.toArray(new String[0]).
Neither. It follows the iteration order of the set, which is hash order. Sort the array with Arrays.sort() if you need order.
No. toArray() copies the references into a new array, so adding to the array or the set does not affect the other. The element objects themselves are shared.
Both give a String[] with the elements. The first lets the method allocate the array; the second reuses your array because it is exactly the right size. Either is fine.