Convert Set of String to Array of String in Java

Given a Set of Strings, the task is to convert the Set into an Array of Strings in Java.
Examples:
Input: Set<String>: ["ForGeeks", "A Computer Portal", "Geeks"] Output: String[]: ["ForGeeks", "A Computer Portal", "Geeks"] Input: Set<String>: ["G", "e", "k", "s"] Output: String[]: ["G", "e", "k", "s"]
- Method 1: Naive Method.
- Get the Set of Strings.
- Create an empty Array of String of size as that of the Set of String.
- Using advanced for loop, copy each element of the Set of String into the Array of String.
- Return or print the Array of String.
Below is the implementation of the above approach:
// Java program to convert// Set of Strings to Array of Stringsimportjava.util.Arrays;importjava.util.Set;importjava.util.HashSet;classGFG {// Function to convert Set<String> to String[]publicstaticString[] convert(Set<String> setOfString){// Create String[] of size of setOfStringString[] arrayOfString =newString[setOfString.size()];// Copy elements from set to string array// using advanced for loopintindex =0;for(String str : setOfString)arrayOfString[index++] = str;// return the formed String[]returnarrayOfString;}publicstaticvoidmain(String[] args){// Get the Set of StringSet<String>setOfString =newHashSet<>(Arrays.asList("Geeks","ForGeeks","A Computer Portal"));// Print the setOfStringSystem.out.println("Set of String: "+ setOfString);// Convert Set to String arrayString[] arrayOfString = convert(setOfString);// Print the arrayOfStringSystem.out.println("Array of String: "+ Arrays.toString(arrayOfString));}}Output:Set of String: [ForGeeks, A Computer Portal, Geeks] Array of String: [ForGeeks, A Computer Portal, Geeks]
- Method 2: Using Set.toArray() method.
- Get the Set of Strings.
- Convert the Set of String to Array of String using Set.toArray() method by passing an
empty array of String type. JVM will allocate memory for string array. - Return or print the Array of String.
Below is the implementation of the above approach:
// Java program to convert// Set of Strings to Array of Stringsimportjava.util.Arrays;importjava.util.Set;importjava.util.HashSet;classGFG {// Function to convert Set<String> to String[]publicstaticString[] convert(Set<String> setOfString){// Create String[] from setOfStringString[] arrayOfString = setOfString.toArray(newString[0]);// return the formed String[]returnarrayOfString;}publicstaticvoidmain(String[] args){// Get the Set of StringSet<String>setOfString =newHashSet<>(Arrays.asList("Geeks","ForGeeks","A Computer Portal"));// Print the setOfStringSystem.out.println("Set of String: "+ setOfString);// Convert Set to String arrayString[] arrayOfString = convert(setOfString);// Print the arrayOfStringSystem.out.println("Array of String: "+ Arrays.toString(arrayOfString));}}Output:Set of String: [ForGeeks, A Computer Portal, Geeks] Array of String: [ForGeeks, A Computer Portal, Geeks]
- Method 3: Using Arrays.copyOf() method.
- Get the Set of Strings.
- Convert the Set of String to Array of String using Arrays.copyOf() method by passing the Set of String, the size of the Set of String, and the desired output type as the String[].
- Return or print the Array of String.
Below is the implementation of the above approach:
// Java program to convert// Set of Strings to Array of Stringsimportjava.util.Arrays;importjava.util.Set;importjava.util.HashSet;classGFG {// Function to convert Set<String> to String[]publicstaticString[] convert(Set<String> setOfString){// Create String[] from setOfStringString[] arrayOfString = Arrays.copyOf(setOfString.toArray(),setOfString.size(),String[].class);// return the formed String[]returnarrayOfString;}publicstaticvoidmain(String[] args){// Get the Set of StringSet<String>setOfString =newHashSet<>(Arrays.asList("Geeks","ForGeeks","A Computer Portal"));// Print the setOfStringSystem.out.println("Set of String: "+ setOfString);// Convert Set to String arrayString[] arrayOfString = convert(setOfString);// Print the arrayOfStringSystem.out.println("Array of String: "+ Arrays.toString(arrayOfString));}}Output:Set of String: [ForGeeks, A Computer Portal, Geeks] Array of String: [ForGeeks, A Computer Portal, Geeks]
- Method 4: Using System.arraycopy() method.
- Get the Set of Strings.
- Convert the Set of String to Array of String using System.arraycopy() method.
- Return or print the Array of String.
Below is the implementation of the above approach:
// Java program to convert// Set of Strings to Array of Stringsimportjava.util.Arrays;importjava.util.Set;importjava.util.HashSet;classGFG {// Function to convert Set<String> to String[]publicstaticString[] convert(Set<String> setOfString){// Create String[] of size of setOfStringString[] arrayOfString =newString[setOfString.size()];// Convert setOfString to String[]System.arraycopy(// sourcesetOfString.toArray(),// from index to be copied from Source0,// DestinationarrayOfString,// From index where to be copied in Destination0,// Number of elements to be copiedsetOfString.size());// return the formed String[]returnarrayOfString;}publicstaticvoidmain(String[] args){// Get the Set of StringSet<String>setOfString =newHashSet<>(Arrays.asList("Geeks","ForGeeks","A Computer Portal"));// Print the setOfStringSystem.out.println("Set of String: "+ setOfString);// Convert Set to String arrayString[] arrayOfString = convert(setOfString);// Print the arrayOfStringSystem.out.println("Array of String: "+ Arrays.toString(arrayOfString));}}Output:Set of String: [ForGeeks, A Computer Portal, Geeks] Array of String: [ForGeeks, A Computer Portal, Geeks]
- Method 5: Using Java 8 Streams.
- Get the Set of Strings.
- Convert the Set of String to Stream using stream() method.
- Convert the Stream to String[] using toArray() method.
- Return or print the Array of String.
Below is the implementation of the above approach:
// Java program to convert// Set of Strings to Array of Stringsimportjava.util.Arrays;importjava.util.Set;importjava.util.HashSet;classGFG {// Function to convert Set<String> to String[]publicstaticString[] convert(Set<String> setOfString){// Create String[] from setOfStringString[] arrayOfString = setOfString// Convert Set of String// to Stream<String>.stream()// Convert Stream<String>// to String[].toArray(String[] ::new);// return the formed String[]returnarrayOfString;}publicstaticvoidmain(String[] args){// Get the Set of StringSet<String>setOfString =newHashSet<>(Arrays.asList("Geeks","ForGeeks","A Computer Portal"));// Print the setOfStringSystem.out.println("Set of String: "+ setOfString);// Convert Set to String arrayString[] arrayOfString = convert(setOfString);// Print the arrayOfStringSystem.out.println("Array of String: "+ Arrays.toString(arrayOfString));}}Output:Set of String: [ForGeeks, A Computer Portal, Geeks] Array of String: [ForGeeks, A Computer Portal, Geeks]



