[ACCEPTED]-Converting from HashSet<String> to String[]-hashset

Accepted answer
Score: 104
set.toArray(new String[set.size()]);

0

Score: 4

Answer of JB Nizet is correct, but in case 3 you did this to transform to a CSV like 2 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 1 allows to bypass array notation (the []).

Score: 0

The JB Nizet's answer is correct. In Java 15, the 1 better answer is:

set.toArray(new String[0]); 

More Related questions