I need to get a String[] out of a Set<String>, but I don’t know how to do it. The following fails:
Map<String, ?> myMap = gpxlist.getAll();
Set<String> myset = myMap.keySet();
String[] GPXFILES1 = (String[]) myset.toArray(); // Here it fails.
How can I fix it so that it works?
Use the
Set#toArray(IntFunction<T[]>)method taking anIntFunctionas generator.If you’re not on Java 11 yet, then use the
Set#toArray(T[])method taking a typed array argument of the same size.While still not on Java 11, and you can’t guarantee that
mysetis unmodifiable at the moment of conversion to array, then better specify an empty typed array.