Home >Java >javaTutorial >How to convert set of strings to array of strings in Java

How to convert set of strings to array of strings in Java

青灯夜游
青灯夜游Original
2019-01-21 14:42:4624953browse

The methods to convert a string set (Set) into a string array (String[]) in Java include: using a for loop, using the Set.toArray() method, using the Arrays.copyOf() method, etc. wait. Let’s introduce it in detail below, hoping it will be helpful to everyone.

How to convert set of strings to array of strings in Java

Method 1: Use for loop

import java.util.Arrays; 
import java.util.Set; 
import java.util.HashSet; 
  
class GFG { 
  
    // 将 字符串集(Set<String>)转换为字符串数组(String[])的函数
    public static String[] convert(Set<String> setOfString) 
    { 
  
        // 创建和字符串集大小相同的一个空字符串数组
        String[] arrayOfString = new String[setOfString.size()]; 
  
        // 使用高级for循环,将字符串集合里的每个元素复制到字符串数组中。
        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("PHP","JAVASCRIPT","HTML")); 
  
        // 输出字符串集合:setofstring
        System.out.println("\n字符串集合为: "
                           + setOfString); 
  
        // 调用convert函数,将集合转换为字符串数组
        String[] arrayOfString = convert(setOfString); 
  
        // Print the arrayOfString 
        System.out.println("\n字符串数组为: "
                           + Arrays.toString(arrayOfString)); 
    } 
}

Output:

How to convert set of strings to array of strings in Java

Instructions:

1. Get the string set.

2. Create an empty string array with the same size as the string set.

3. Use an advanced for loop to copy each element in the string collection to a string array.

4. Output string array.

Method 2: Use the Set.toArray() method

Modify the convert() function based on method 1 to convert ["php","javascript","css"]Collection

// 将 字符串集(Set<String>)转换为字符串数组(String[])的函数
public static String[] convert(Set<String> setOfString) 
{ 
  
  // 通过setOfString创建字符串[]
  String[] arrayOfString = setOfString.toArray(new String[0]); 
  
  // 返回字符串数组
  return arrayOfString;
}

Output:

How to convert set of strings to array of strings in Java

Description:

1. Get the string set.

2. By passing an empty array of String type, use the Set.toArray() method to convert the string set into a string array. JVM will allocate memory for the string array.

3. Output string array.

Method 3: Use Arrays.copyOf() method

Modify the convert() function based on method 1

// 将 字符串集(Set<String>)转换为字符串数组(String[])的函数
public static String[] convert(Set<String> setOfString) 
{ 
  
   // 通过setOfString创建字符串数组
   String[] arrayOfString = Arrays.copyOf(setOfString.toArray(),setOfString.size(),String[].class);
  
   // 返回字符串数组
   return arrayOfString;
 }

Output:

How to convert set of strings to array of strings in Java

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to convert set of strings to array of strings in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn