📌  相关文章
📜  在Java中将字符串集合转换为字符串数组

📅  最后修改于: 2022-05-13 01:55:43.293000             🧑  作者: Mango

在Java中将字符串集合转换为字符串数组

给定一组字符串,任务是将 Set 转换为Java中的字符串数组。

例子:

Input: Set: ["ForGeeks", "A Computer Portal", "Geeks"]
Output: String[]: ["ForGeeks", "A Computer Portal", "Geeks"]

Input: Set: ["G", "e", "k", "s"]
Output: String[]: ["G", "e", "k", "s"]
  • 方法1:朴素方法。
    1. 获取字符串集。
    2. 创建一个空的字符串数组,其大小与字符串集的大小相同。
    3. 使用高级 for 循环,将字符串集合的每个元素复制到字符串数组中。
    4. 返回或打印字符串数组。

    下面是上述方法的实现:

    // 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 to String[]
        public static String[] convert(Set 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
                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));
        }
    }
    
    输出:
    Set of String: [ForGeeks, A Computer Portal, Geeks]
    Array of String: [ForGeeks, A Computer Portal, Geeks]
    
  • 方法 2 :使用 Set.toArray() 方法。
    1. 获取字符串集。
    2. 使用 Set.toArray() 方法通过传递一个字符串将字符串集合转换为字符串数组
      String 类型的空数组。 JVM 将为字符串数组分配内存。
    3. 返回或打印字符串数组。

    下面是上述方法的实现:

    // 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 to String[]
        public static String[] convert(Set 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
                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));
        }
    }
    
    输出:
    Set of String: [ForGeeks, A Computer Portal, Geeks]
    Array of String: [ForGeeks, A Computer Portal, Geeks]
    
  • 方法 3 :使用 Arrays.copyOf() 方法。
    1. 获取字符串集。
    2. 使用 Arrays.copyOf() 方法将字符串集合转换为字符串数组,方法是将字符串集合、字符串集合的大小和所需的输出类型作为字符串 [] 传递。
    3. 返回或打印字符串数组。

    下面是上述方法的实现:

    // 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 to String[]
        public static String[] convert(Set 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
                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));
        }
    }
    
    输出:
    Set of String: [ForGeeks, A Computer Portal, Geeks]
    Array of String: [ForGeeks, A Computer Portal, Geeks]
    
  • 方法 4 :使用 System.arraycopy() 方法。
    1. 获取字符串集。
    2. 使用 System.arraycopy() 方法将字符串集合转换为字符串数组。
    3. 返回或打印字符串数组。

    下面是上述方法的实现:

    // 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 to String[]
        public static String[] convert(Set 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
                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));
        }
    }
    
    输出:
    Set of String: [ForGeeks, A Computer Portal, Geeks]
    Array of String: [ForGeeks, A Computer Portal, Geeks]
    
  • 方法 5 :使用Java 8 流。
    1. 获取字符串集。
    2. 将字符串集转换为流使用流()方法。
    3. 转换流使用 toArray() 方法到 String[]。
    4. 返回或打印字符串数组。

    下面是上述方法的实现:

    // 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 to String[]
        public static String[] convert(Set setOfString)
        {
      
            // Create String[] from setOfString
            String[] arrayOfString = setOfString
      
                                         // Convert Set of String
                                         // to Stream
                                         .stream()
      
                                         // Convert Stream
                                         // to String[]
                                         .toArray(String[] ::new);
      
            // return the formed String[]
            return arrayOfString;
        }
      
        public static void main(String[] args)
        {
      
            // Get the Set of String
            Set
                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));
        }
    }
    
    输出:
    Set of String: [ForGeeks, A Computer Portal, Geeks]
    Array of String: [ForGeeks, A Computer Portal, Geeks]