Convert Set of String to a String array in Java
This post will discuss how to convert a set of string to a string array in Java.
1. Naive solution
A naive solution is to use a for-each loop to iterate over the given set and simply copy elements from set to string array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Arrays; import java.util.Set; import java.util.HashSet; class Main { // Program to convert `Set<String>` to a string array in Java public static void main(String[] args) { Set<String> set = new HashSet<>(Arrays.asList("C++", "Java")); // allocate memory for string array String[] array = new String[set.size()]; // copy elements from set to string array int i = 0; for (String s: set) { array[i++] = s; } System.out.println(Arrays.toString(array)); } } |
Output:
[Java, C++]
2. Using Set.toArray() method
We can use the toArray(T[]) method to copy the set into a newly allocated string array. We can either pass a string array as an argument to the toArray() method or pass an empty string type array. If an empty array is passed, JVM will allocate memory for the string array. Please note that if no argument is passed to toArray(), it will return an Object array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.Set; import java.util.HashSet; class Main { // Program to convert `Set<String>` to a string array in Java public static void main(String[] args) { Set<String> set = new HashSet<>(Arrays.asList("C++", "Java")); String[] array = set.toArray(new String[0]); System.out.println(Arrays.toString(array)); } } |
Output:
[Java, C++]
3. Using Arrays.copyOf() method
We can also use Arrays.copyOf() to copy the specified array to an array of the specified type.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.Set; import java.util.HashSet; class Main { // Program to convert `Set<String>` to a string array in Java public static void main(String[] args) { Set<String> set = new HashSet<>(Arrays.asList("C++", "Java")); String[] array = Arrays.copyOf(set.toArray(), set.size(), String[].class); System.out.println(Arrays.toString(array)); } } |
Output:
[Java, C++]
4. Using System.arraycopy() method
We can use System.arraycopy() that copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.Set; import java.util.HashSet; class Main { // Program to convert `Set<String>` to a string array in Java public static void main(String[] args) { Set<String> set = new HashSet<>(Arrays.asList("C++", "Java")); String[] array = new String[set.size()]; System.arraycopy(set.toArray(), 0, array, 0, set.size()); System.out.println(Arrays.toString(array)); } } |
Output:
[Java, C++]
5. Using Java 8
Java 8 provides another simple approach to convert Set to a string array. First, we convert the specified set of strings to a sequential Stream and then use the toArray() method to accumulate the stream elements into a new string array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.Set; import java.util.HashSet; class Main { // Program to convert `Set<String>` to a string array in Java 8 and above public static void main(String[] args) { Set<String> set = new HashSet<>(Arrays.asList("C++", "Java")); String[] array = set.stream().toArray(String[]::new); System.out.println(Arrays.toString(array)); } } |
Output:
[Java, C++]
That’s all about converting Set of String to an array of Java String.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)