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:
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class GFG {
public static String[] convert(Set<String> setOfString)
{
String[] arrayOfString = new String[setOfString.size()];
int index = 0 ;
for (String str : setOfString)
arrayOfString[index++] = str;
return arrayOfString;
}
public static void main(String[] args)
{
Set<String>
setOfString = new HashSet<>(
Arrays.asList( "Geeks" ,
"ForGeeks" ,
"A Computer Portal" ));
System.out.println( "Set of String: "
+ setOfString);
String[] arrayOfString = convert(setOfString);
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:
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class GFG {
public static String[] convert(Set<String> setOfString)
{
String[] arrayOfString = setOfString
.toArray( new String[ 0 ]);
return arrayOfString;
}
public static void main(String[] args)
{
Set<String>
setOfString = new HashSet<>(
Arrays.asList( "Geeks" ,
"ForGeeks" ,
"A Computer Portal" ));
System.out.println( "Set of String: "
+ setOfString);
String[] arrayOfString = convert(setOfString);
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:
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class GFG {
public static String[] convert(Set<String> setOfString)
{
String[] arrayOfString = Arrays
.copyOf(
setOfString.toArray(),
setOfString.size(),
String[]. class );
return arrayOfString;
}
public static void main(String[] args)
{
Set<String>
setOfString = new HashSet<>(
Arrays.asList( "Geeks" ,
"ForGeeks" ,
"A Computer Portal" ));
System.out.println( "Set of String: "
+ setOfString);
String[] arrayOfString = convert(setOfString);
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:
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class GFG {
public static String[] convert(Set<String> setOfString)
{
String[] arrayOfString = new String[setOfString.size()];
System.arraycopy(
setOfString.toArray(),
0 ,
arrayOfString,
0 ,
setOfString.size());
return arrayOfString;
}
public static void main(String[] args)
{
Set<String>
setOfString = new HashSet<>(
Arrays.asList( "Geeks" ,
"ForGeeks" ,
"A Computer Portal" ));
System.out.println( "Set of String: "
+ setOfString);
String[] arrayOfString = convert(setOfString);
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:
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class GFG {
public static String[] convert(Set<String> setOfString)
{
String[] arrayOfString = setOfString
.stream()
.toArray(String[] :: new );
return arrayOfString;
}
public static void main(String[] args)
{
Set<String>
setOfString = new HashSet<>(
Arrays.asList( "Geeks" ,
"ForGeeks" ,
"A Computer Portal" ));
System.out.println( "Set of String: "
+ setOfString);
String[] arrayOfString = convert(setOfString);
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]
Last Updated :
11 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...