How to convert Set to String[]?

前端 未结 7 1828
栀梦
栀梦 2020-11-28 19:12

I need to get a String[] out of a Set, but I don\'t know how to do it. The following fails:

Map myMa         


        
相关标签:
7条回答
  • 2020-11-28 19:19

    Java 11

    The new default toArray method in Collection interface allows the elements of the collection to be transferred to a newly created array of the desired runtime type. It takes IntFunction<T[]> generator as argument and can be used as:

     String[] array = set.toArray(String[]::new);
    

    There is already a similar method Collection.toArray(T[]) and this addition means we no longer be able to pass null as argument because in that case reference to the method would be ambiguous. But it is still okay since both methods throw a NPE anyways.

    Java 8

    In Java 8 we can use streams API:

    String[] array = set.stream().toArray(String[]::new);
    

    We can also make use of the overloaded version of toArray() which takes IntFunction<A[]> generator as:

    String[] array = set.stream().toArray(n -> new String[n]);
    

    The purpose of the generator function here is to take an integer (size of desired array) and produce an array of desired size. I personally prefer the former approach using method reference than the later one using lambda expression.

    0 讨论(0)
  • 2020-11-28 19:24

    In Java 11 we can use Collection.toArray(generator) method. The following code will create a new array of String:

    Set<String> set = Set.of("one", "two", "three");
    String[] array = set.toArray(String[]::new)
    

    See: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#toArray(java.util.function.IntFunction)

    0 讨论(0)
  • 2020-11-28 19:30
    Set<String> stringSet= new HashSet<>();
    String[] s = (String[])stringSet.toArray();
    
    0 讨论(0)
  • 2020-11-28 19:31

    Guava style:

    Set<String> myset = myMap.keySet();
    FluentIterable.from(mySet).toArray(String.class);
    

    more info: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/FluentIterable.html

    0 讨论(0)
  • 2020-11-28 19:33

    Use the Set#toArray(IntFunction<T[]>) method taking an IntFunction as generator.

    String[] GPXFILES1 = myset.toArray(String[]::new);
    

    If you're not on Java 11 yet, then use the Set#toArray(T[]) method taking a typed array argument of the same size.

    String[] GPXFILES1 = myset.toArray(new String[myset.size()]);
    

    While still not on Java 11, and you can't guarantee that myset is unmodifiable at the moment of conversion to array, then better specify an empty typed array.

    String[] GPXFILES1 = myset.toArray(new String[0]);
    
    0 讨论(0)
  • 2020-11-28 19:39

    I was facing the same situation.

    I begin by declaring the structures I need:

    Set<String> myKeysInSet = null; String[] myArrayOfString = null;

    In my case, I have a JSON object and I need all the keys in this JSON to be stored in an array of strings. Using the GSON library, I use JSON.keySet() to get the keys and move to my Set :

    myKeysInSet = json_any.keySet();

    With this, I have a Set structure with all the keys, as I needed it. So I just need to the values to my Array of Strings. See the code below:

    myArrayOfString = myKeysInSet.toArray(new String[myKeysInSet.size()]);

    This was my first answer in StackOverflow. Sorry for any error :D

    0 讨论(0)
提交回复
热议问题