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 StringsÂÂimportjava.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 setOfString       ÂString[] arrayOfString =newString[setOfString.size()];       Â// Copy elements from set to string array       Â// using advanced for loop       Âintindex =0;       Âfor(String str : setOfString)           ÂarrayOfString[index++] = str;       Â// return the formed String[]       ÂreturnarrayOfString;   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the Set of String       ÂSet<String>           ÂsetOfString =newHashSet<>(               ÂArrays.asList("Geeks",                             Â"ForGeeks",                             Â"A Computer Portal"));       Â// Print the setOfString       ÂSystem.out.println("Set of String: "                          Â+ setOfString);       Â// Convert Set to String array       ÂString[] arrayOfString = convert(setOfString);       Â// Print the arrayOfString       ÂSystem.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 StringsÂÂimportjava.util.Arrays;importjava.util.Set;importjava.util.HashSet;ÂÂclassGFG {   Â// Function to convert Set<String> to String[]   ÂpublicstaticString[] convert(Set<String> setOfString)   Â{       Â// Create String[] from setOfString       ÂString[] arrayOfString = setOfString                                    Â.toArray(newString[0]);       Â// return the formed String[]       ÂreturnarrayOfString;   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the Set of String       ÂSet<String>           ÂsetOfString =newHashSet<>(               ÂArrays.asList("Geeks",                             Â"ForGeeks",                             Â"A Computer Portal"));       Â// Print the setOfString       ÂSystem.out.println("Set of String: "                          Â+ setOfString);       Â// Convert Set to String array       ÂString[] arrayOfString = convert(setOfString);       Â// Print the arrayOfString       ÂSystem.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 StringsÂÂimportjava.util.Arrays;importjava.util.Set;importjava.util.HashSet;ÂÂclassGFG {   Â// Function to convert Set<String> to String[]   ÂpublicstaticString[] convert(Set<String> setOfString)   Â{       Â// Create String[] from setOfString       ÂString[] arrayOfString = Arrays                                    Â.copyOf(                                        ÂsetOfString.toArray(),                                        ÂsetOfString.size(),                                        ÂString[].class);       Â// return the formed String[]       ÂreturnarrayOfString;   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the Set of String       ÂSet<String>           ÂsetOfString =newHashSet<>(               ÂArrays.asList("Geeks",                             Â"ForGeeks",                             Â"A Computer Portal"));       Â// Print the setOfString       ÂSystem.out.println("Set of String: "                          Â+ setOfString);       Â// Convert Set to String array       ÂString[] arrayOfString = convert(setOfString);       Â// Print the arrayOfString       ÂSystem.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 StringsÂÂimportjava.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 setOfString       ÂString[] arrayOfString =newString[setOfString.size()];       Â// Convert setOfString to String[]       ÂSystem.arraycopy(           Â// source           ÂsetOfString.toArray(),           Â// from index to be copied from Source           Â0,           Â// Destination           ÂarrayOfString,           Â// From index where to be copied in Destination           Â0,           Â// Number of elements to be copied           ÂsetOfString.size());       Â// return the formed String[]       ÂreturnarrayOfString;   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the Set of String       ÂSet<String>           ÂsetOfString =newHashSet<>(               ÂArrays.asList("Geeks",                             Â"ForGeeks",                             Â"A Computer Portal"));       Â// Print the setOfString       ÂSystem.out.println("Set of String: "                          Â+ setOfString);       Â// Convert Set to String array       ÂString[] arrayOfString = convert(setOfString);       Â// Print the arrayOfString       ÂSystem.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 StringsÂÂimportjava.util.Arrays;importjava.util.Set;importjava.util.HashSet;ÂÂclassGFG {   Â// Function to convert Set<String> to String[]   ÂpublicstaticString[] convert(Set<String> setOfString)   Â{       Â// Create String[] from setOfString       ÂString[] 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 String       ÂSet<String>           ÂsetOfString =newHashSet<>(               ÂArrays.asList("Geeks",                             Â"ForGeeks",                             Â"A Computer Portal"));       Â// Print the setOfString       ÂSystem.out.println("Set of String: "                          Â+ setOfString);       Â// Convert Set to String array       ÂString[] arrayOfString = convert(setOfString);       Â// Print the arrayOfString       ÂSystem.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]


