首页 文章资讯内容详情

在 Java 中将字符串转换为逗号分隔的 List(列表)

2026-06-04 1 花语

首先,设置一个具有字符串值的列表-

List<String> myList = new ArrayList<>( Arrays.asList("One", "Two", "Three", "Four"));

现在,使用String.join()将它们设置为逗号分隔的列表-

String str = String.join(", ", myList);

示例

以下是在Java中将字符串转换为逗号分隔列表的程序-

import java.util.*; public class Demo { public static void main(String args[]) { List<String> myList = new ArrayList<>(Arrays.asList("One", "Two", "Three", "Four")); System.out.println("List = " + myList); //逗号分隔 String str = String.join(", ", myList); System.out.println("String (Comma Separated) = " + str); } }

输出结果

List = [One, Two, Three, Four] Comma separated String: One, Two, Three, Four