Wednesday, July 3, 2024
HomeLanguagesJavaConvert Set of String to Array of String in Java

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.
    1. Get the Set of Strings.
    2. Create an empty Array of String of size as that of the Set of String.
    3. Using advanced for loop, copy each element of the Set of String into the Array of String.
    4. 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
      
    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.
    1. Get the Set of Strings.
    2. 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.
    3. 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
      
    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.
    1. Get the Set of Strings.
    2. 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[].
    3. 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
      
    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.
    1. Get the Set of Strings.
    2. Convert the Set of String to Array of String using System.arraycopy() method.
    3. 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
      
    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.
    1. Get the Set of Strings.
    2. Convert the Set of String to Stream using stream() method.
    3. Convert the Stream to String[] using toArray() method.
    4. 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
      
    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]
    

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments