A Set in Java stores unique elements, while a String[] is a fixed-size array used to store strings. Converting a Set<String> to a String[] is useful when array-based operations or APIs are required. Java provides methods such as toArray() and the Stream API to perform this conversion.
- HashSet does not guarantee the order of elements, while LinkedHashSet preserves insertion order.
- The toArray(new String[0]) approach is simple and commonly used to convert a Set<String> into a String[].
Method to Convert Set of Strings to String Array
1. Using a for Loop
In this approach, a String array with the same size as the Set is created. Each element of the Set is then copied into the array using an enhanced for loop.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static String[] convert(Set<String> set) {
// Create an array with the same size as the Set
String[] array = new String[set.size()];
// Copy Set elements into the array
int index = 0;
for (String value : set) {
array[index++] = value;
}
return array;
}
public static void main(String[] args) {
Set<String> set = new HashSet<>(
Arrays.asList(
"Java",
"Python",
"C++"
)
);
System.out.println("Set of Strings: " + set);
String[] array = convert(set);
System.out.println("String Array: "
+ Arrays.toString(array));
}
}
Output
Set of Strings: [Java, C++, Python] String Array: [Java, C++, Python]
Explanation: The program creates a String array based on the size of the Set and copies each Set element into the array. The resulting array is then displayed using Arrays.toString().
2. Using Set.toArray() Method
The toArray() method provided by the Collection interface can convert the elements of a Set into an array. By passing new String[0], the method returns an array of type String[].
Syntax:
String[] array = set.toArray(new String[0]);
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static String[] convert(Set<String> set) {
// Convert Set to String array
return set.toArray(new String[0]);
}
public static void main(String[] args) {
Set<String> set = new HashSet<>(
Arrays.asList(
"Java",
"Python",
"C++"
)
);
System.out.println("Set of Strings: " + set);
String[] array = convert(set);
System.out.println("String Array: "
+ Arrays.toString(array));
}
}
Output
Set of Strings: [Java, C++, Python] String Array: [Java, C++, Python]
Explanation: The toArray(new String[0]) method converts the Set directly into a String[]. This is one of the simplest and most commonly used approaches.
3. Using Arrays.copyOf()
The Arrays.copyOf() method can be used to copy the elements of an array into another array of a specified type and size. First, the Set is converted into an Object array using toArray(), and then it is converted into a String array.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static String[] convert(Set<String> set) {
// Convert Set to Object array
Object[] objectArray = set.toArray();
// Convert Object array to String array
return Arrays.copyOf(
objectArray,
objectArray.length,
String[].class
);
}
public static void main(String[] args) {
Set<String> set = new HashSet<>(
Arrays.asList(
"Java",
"Python",
"C++"
)
);
System.out.println("Set of Strings: " + set);
String[] array = convert(set);
System.out.println("String Array: "
+ Arrays.toString(array));
}
}
Output
Set of Strings: [Java, C++, Python] String Array: [Java, C++, Python]
Explanation: The Set is first converted into an Object[] using toArray(). Arrays.copyOf() then creates a new array of type String[] containing the same elements.
4. Using System.arraycopy()
The System.arraycopy() method can copy elements from one array to another. In this approach, the Set is first converted into an array, and its elements are then copied into a String array.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static String[] convert(Set<String> set) {
// Create the destination String array
String[] array = new String[set.size()];
// Convert Set to Object array
Object[] source = set.toArray();
// Copy elements into String array
System.arraycopy(
source, 0,
array, 0,
source.length
);
return array;
}
public static void main(String[] args) {
Set<String> set = new HashSet<>(
Arrays.asList(
"Java",
"Python",
"C++"
)
);
System.out.println("Set of Strings: " + set);
String[] array = convert(set);
System.out.println("String Array: "
+ Arrays.toString(array));
}
}
Output
Set of Strings: [Java, C++, Python] String Array: [Java, C++, Python]
Explanation: The Set is first converted into an Object[]. The System.arraycopy() method then copies these elements into the newly created String[].
5. Using Java 8 Streams
The Stream API provides a concise way to convert a Set into a String array. The stream() method creates a Stream from the Set, while toArray(String[]::new) converts the Stream into a String array.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static String[] convert(Set<String> set) {
// Convert Set to Stream and then to String array
return set.stream()
.toArray(String[]::new);
}
public static void main(String[] args) {
Set<String> set = new HashSet<>(
Arrays.asList(
"Java",
"Python",
"C++"
)
);
System.out.println("Set of Strings: " + set);
String[] array = convert(set);
System.out.println("String Array: "
+ Arrays.toString(array));
}
}
Output
Set of Strings: [Java, C++, Python] String Array: [Java, C++, Python]
Explanation: The stream() method converts the Set into a Stream, and toArray(String[]::new) creates a String[] containing the Stream elements. This approach is particularly useful when other Stream operations are required before creating the array.
Note: A HashSet does not guarantee insertion order. If the order of elements must remain the same as the original Set, use LinkedHashSet.