在Java中将字符串Set集合转换为逗号分隔的字符串
让我们首先创建一个具有字符串值的集合-
Set<String> set = new HashSet<>(Arrays.asList("One", "Two", "Three", "Four", "Five", "Six"));
现在,使用String.join()将其转换为逗号分隔的字符串-
String str = String.join(", ", set);
示例
以下是在Java中将字符串集转换为逗号分隔的字符串的程序-
import java.util.*;public class Demo {
public static void main(String args[]) {
Set<String> set = new HashSet<>(Arrays.asList("One", "Two", "Three", "Four", "Five", "Six"));
System.out.println("Set = " + set);
String str = String.join(", ", set);
System.out.println("Comma separated String: "+ str);
}
}
输出结果
Set = [Five, Six, One, Four, Two, Three]Comma separated String: Five, Six, One, Four, Two, Three
以上是 在Java中将字符串Set集合转换为逗号分隔的字符串 的全部内容, 来源链接: utcz.com/z/321510.html