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));}}chevron_rightfilter_noneOutput: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));}}chevron_rightfilter_noneOutput: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));}}chevron_rightfilter_noneOutput: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));}}chevron_rightfilter_noneOutput: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));}}chevron_rightfilter_noneOutput:Set of String: [ForGeeks, A Computer Portal, Geeks] Array of String: [ForGeeks, A Computer Portal, Geeks]
Recommended Posts:
- Convert an ArrayList of String to a String array in Java
- Convert a List of String to a comma separated String in Java
- Convert a Set of String to a comma separated String in Java
- Print all possible ways to convert one string into another string | Edit-Distance
- Convert String to Date in Java
- Convert String to Double in Java
- Program to convert set of String to set of Integer in Java
- Program to convert String to IntStream in Java
- Convert a String to a List of Characters in Java
- Program to convert IntStream to String in Java
- Program to Convert Set of Integer to Set of String in Java
- Convert List of Characters to String in Java
- Convert Hexadecimal value String to ASCII value String
- Convert Java Object to Json String using Jackson API
- Convert Json String to Java Object Using GSON
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



