Convert Set of String to Array of String in Java
Last Updated :
11 Dec, 2018
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
// Java program to convert
// Set of Strings to Array of Strings
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class GFG {
// Function to convert Set<String> to String[]
public static String[] convert(Set<String> setOfString)
{
// Create String[] of size of setOfString
String[] arrayOfString = new String[setOfString.size()];
// Copy elements from set to string array
// using advanced for loop
int index = 0;
for (String str : setOfString)
arrayOfString[index++] = str;
// return the formed String[]
return arrayOfString;
}
public static void main(String[] args)
{
// Get the Set of String
Set<String>
setOfString = new HashSet<>(
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
// Java program to convert
// Set of Strings to Array of Strings
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class GFG {
// Function to convert Set<String> to String[]
public static String[] convert(Set<String> setOfString)
{
// Create String[] from setOfString
String[] arrayOfString = setOfString
.toArray(new String[0]);
// return the formed String[]
return arrayOfString;
}
public static void main(String[] args)
{
// Get the Set of String
Set<String>
setOfString = new HashSet<>(
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
// Java program to convert
// Set of Strings to Array of Strings
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class GFG {
// Function to convert Set<String> to String[]
public static String[] convert(Set<String> setOfString)
{
// Create String[] from setOfString
String[] arrayOfString = Arrays
.copyOf(
setOfString.toArray(),
setOfString.size(),
String[].class);
// return the formed String[]
return arrayOfString;
}
public static void main(String[] args)
{
// Get the Set of String
Set<String>
setOfString = new HashSet<>(
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
// Java program to convert
// Set of Strings to Array of Strings
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class GFG {
// Function to convert Set<String> to String[]
public static String[] convert(Set<String> setOfString)
{
// Create String[] of size of setOfString
String[] arrayOfString = new String[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[]
return arrayOfString;
}
public static void main(String[] args)
{
// Get the Set of String
Set<String>
setOfString = new HashSet<>(
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
// Java program to convert
// Set of Strings to Array of Strings
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class GFG {
// Function to convert Set<String> to String[]
public static String[] 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[]
return arrayOfString;
}
public static void main(String[] args)
{
// Get the Set of String
Set<String>
setOfString = new HashSet<>(
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]