Converting from HashSet<String> to String[]

断了今生、忘了曾经 提交于 2019-12-20 09:57:54

问题


What's the best way to convert HashSet<String> to String[]?


回答1:


set.toArray(new String[set.size()]);



回答2:


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 arrays.



来源:https://stackoverflow.com/questions/5474656/converting-from-hashsetstring-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!