What\'s the best way to convert HashSet
to String[]
?
The JB Nizet's answer is correct. In Java 15, the better answer is:
set.toArray(new String[0]);
set.toArray(new String[set.size()]);
Answer of JB Nizet is correct, but in case you did this to transform to a CSV like string, with Java 8 you can now do:
Set<String> mySet = new HashSet<>(Arrays.asList("a", "b", "c"));
System.out.println(String.join(", ", mySet));
Output is: a, b, c
This allows to bypass array notation (the []
).