Converting from HashSet to String[]

前端 未结 3 1505
清歌不尽
清歌不尽 2021-02-02 06:59

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

3条回答
  •  失恋的感觉
    2021-02-02 07:35

    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 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 []).

提交回复
热议问题