Java 如何转换列表<;字符串>;转换为逗号分隔的字符串,而不显式迭代列表 List id=new ArrayList(); 同上,添加(“1”); 同上,添加(“2”); 同上,添加(“3”); 同上,添加(“4”);

Java 如何转换列表<;字符串>;转换为逗号分隔的字符串,而不显式迭代列表 List id=new ArrayList(); 同上,添加(“1”); 同上,添加(“2”); 同上,添加(“3”); 同上,添加(“4”);,java,list,Java,List,现在我希望这个列表的输出为1,2,3,4,而不显式地迭代它 List<String> ids = new ArrayList<String>(); ids.add("1"); ids.add("2"); ids.add("3"); ids.add("4"); 或者您可以使用StringUtils: import com.google.common.base.Joiner; Joiner.on(",").join(ids); 公共静态字符串联接(对象[]数组, 煤焦分

现在我希望这个列表的输出为1,2,3,4,而不显式地迭代它

List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
ids.add("4");
或者您可以使用StringUtils

import com.google.common.base.Joiner;

Joiner.on(",").join(ids);
公共静态字符串联接(对象[]数组,
煤焦分离器)
公共静态字符串联接(Iterable迭代器,
煤焦分离器)
将提供的数组/iterable的元素联接到包含提供的元素列表的单个字符串中

以下内容:

   public static String join(Object[] array,
                              char separator)

   public static String join(Iterable<?> iterator,
                              char separator)
将给您一个逗号分隔的列表

您需要进行一些后处理以删除方括号,但不要太复杂。

使用Java 8:

String joinedString = ids.toString()
对于Java 7-,有一种肮脏的方式(注意:只有在列表中不插入包含
,“
”的字符串时,它才起作用)
显然,
list#toString
将执行循环以创建
idList
,但它不会出现在代码中:

String csv = String.join(",", ids);
List id=new ArrayList();
同上,添加(“1”);
同上,添加(“2”);
同上,添加(“3”);
同上,添加(“4”);
字符串idList=ids.toString();
字符串csv=idList.substring(1,idList.length()-1)。替换(“,”,“,”);

如果要将列表转换为CSV格式………

List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
ids.add("4");
String idList = ids.toString();
String csv = idList.substring(1, idList.length() - 1).replace(", ", ",");
List id=new ArrayList();
同上,添加(“1”);
同上,添加(“2”);
同上,添加(“3”);
同上,添加(“4”);
//CSV格式
字符串csv=ids.toString().replace(“[”,”).replace(“]”,”)
.替换(“,”,“,”);
//由单引号包围的CSV格式
//对于查询中的SQL非常有用
字符串csvWithQuote=ids.toString().replace(“[”,“”).replace(“]”,“”)
.替换(“,”,“,”);
如果您正在使用(以前是GS Collections),则可以使用
makeString()
方法

List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
ids.add("4");

// CSV format
String csv = ids.toString().replace("[", "").replace("]", "")
            .replace(", ", ",");

// CSV format surrounded by single quote 
// Useful for SQL IN QUERY

String csvWithQuote = ids.toString().replace("[", "'").replace("]", "'")
            .replace(", ", "','");

注意:我是Eclipse集合的提交者。

我有一个字符串的ArrayList,我需要将其转换为逗号分隔的列表,没有空格。ArrayList toString()方法添加方括号、逗号和空格。我尝试了下面的正则表达式方法

Assert.assertEquals("1,2,3,4", FastList.newListWith(1, 2, 3, 4).makeString(","));
List myProductList=new ArrayList();
myProductList.add(“sanjay”);
myProductList.add(“sameer”);
myProductList.add(“anand”);
Log.d(“TEST1”,myProductList.toString());//“[sanjay,sameer,anand]”
字符串模式String=myProductList.toString().replaceAll(“[\\s\\[\\]]”,“”);
Log.d(“测试”,模式字符串);//“桑杰,萨米尔,阿南”
请评论更有效的逻辑。 (代码是针对Android/Java的)

谢谢。

最快的方法是

List<String> myProductList = new ArrayList<String>();
myProductList.add("sanjay");
myProductList.add("sameer");
myProductList.add("anand");
Log.d("TEST1", myProductList.toString());     // "[sanjay, sameer, anand]"
String patternString = myProductList.toString().replaceAll("[\\s\\[\\]]", "");
Log.d("TEST", patternString);                 // "sanjay,sameer,anand"
一行程序(纯Java

关于Android的使用:

list.toString().replace(", ", ",").replaceAll("[\\[.\\]]", "");

下面给出了将列表转换为逗号分隔字符串而不显式迭代列表的代码,因为您必须创建一个列表并在其中添加项,而不是将其转换为逗号分隔字符串

此代码的输出将是:Veeru、Nikhil、Ashish、Paritosh

而不是列表的输出[Veeru、Nikhil、Ashish、Paritosh]

android.text.TextUtils.join(",", ids);
字符串列表\u名称;
List myNameList=新建ArrayList();
我的名字列表。添加(“Veeru”);
myNameList.add(“Nikhil”);
我的名字列表。添加(“Ashish”);
myNameList.add(“Paritosh”);
List\u name=myNameList.toString()。替换(“[”,“”)
.替换(“],”)。替换(“,”,“,”);

Java 8解决方案如果不是字符串的集合:

String List_name;
List<String> myNameList = new ArrayList<String>();
myNameList.add("Veeru");
myNameList.add("Nikhil");
myNameList.add("Ashish");
myNameList.add("Paritosh");

List_name = myNameList.toString().replace("[", "")
                    .replace("]", "").replace(", ", ",");

连接/concat拆分数组列表上的函数

import com.google.common.base.Joiner;

Joiner.on(",").join(ids);
使用逗号(“”)将/concat数组列表的所有元素连接到字符串

{Any collection}.stream()
    .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
    .toString()
List id=new ArrayList();
同上,添加(“1”);
同上,添加(“2”);
同上,添加(“3”);
同上,添加(“4”);
字符串allIds=TextUtils.join(“,”,id);
Log.i(“结果”,allIds);
使用逗号(“”)将字符串的所有元素拆分为数组列表

String allIds=“1,2,3,4”;
字符串[]allIdsArray=TextUtils.split(allIds,“,”);
ArrayList idsList=新的ArrayList(Arrays.asList(AllidArray));
for(字符串元素:idsList){
Log.i(“结果”,要素);
}

完成

如果对象下面有属性,则可以使用下面的代码

String allIds = "1,2,3,4";
String[] allIdsArray = TextUtils.split(allIds, ",");
ArrayList<String> idsList = new ArrayList<String>(Arrays.asList(allIdsArray));
for(String element : idsList){
    Log.i("Result", element);
}
String GetCommonDepratedString(列出actionObjects){
StringBuffer sb=新的StringBuffer();
用于(ActionObject ActionObject:actionObjects){
sb.append(actionObject.Id);
}
sb.deleteCharAt(sb.lastIndexOf(“,”);
使某人返回字符串();
}

定义“完全迭代”,因为无论是上帝、克图鲁还是飞行的意大利面怪物,如果不以某种方式迭代所有项目,都无法从列表中取出项目。如果不迭代?:你的应用程序如何在不迭代的情况下获取每个元素的内容?我不想这样做-字符串commaSepIds=“”;对于(String text:ids){commaSepIds=commaSepIds+”,“++text;}Chuck Norris可以做到这一点。即使Jack Bauer也是如此……它也可以是这样的-String csv=idList.substring(1,(idList.length()-1));这取决于你是否想保留逗号后的空格。+1-Joiner使用iterable或object数组,可以跳过输入中的空值。
StringUtils。join
也接受
集合
迭代器
对象。非常好,没有任何第三方库,聪明!仅当列表中的字符串不包含任何
[,]
,并且希望java不会更改列表中的to字符串时才有效。如果不在生产系统中使用它,这是一种危险的方法。不要忘记导入
StringUtils
import org.apache.commons.lang3.StringUtils
commons-lang3是一个很好的字符串相关方法库。StringUtils用于win!”s
StringUtils.join(*)
方法被弃用而支持。该死,我真希望我能把它提高几次!帮了我好几次!!:)这个答案仅限于ANDROID!使用StringUtils answ
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
ids.add("4");
String allIds = TextUtils.join(",", ids);
Log.i("Result", allIds);
String allIds = "1,2,3,4";
String[] allIdsArray = TextUtils.split(allIds, ",");
ArrayList<String> idsList = new ArrayList<String>(Arrays.asList(allIdsArray));
for(String element : idsList){
    Log.i("Result", element);
}
String getCommonSeperatedString(List<ActionObject> actionObjects) {
    StringBuffer sb = new StringBuffer();
    for (ActionObject actionObject : actionObjects){
        sb.append(actionObject.Id).append(",");
    }
    sb.deleteCharAt(sb.lastIndexOf(","));
    return sb.toString();
}