java 如何将 List<Object> 转换为逗号分隔的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48605130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:07:19  来源:igfitidea点击:

How to convert List<Object> into comma separated String

javasqlibatis

提问by user9193174

I am getting list of Addressobjects from the DB call.

我正在Address从数据库调用中获取对象列表。

ArrayList<Address> addresses = new ArrayList<>();

Each Addresshas an int addressIdproperty.

每个人Address都有一个int addressId属性。

I am writing an update query where in the INclause I am sending this whole list of Addressobjects and I am getting ibatis TypeException. How can I convert List<Address>to a comma separated string which can be sent to update query?

我正在写一个更新查询,在IN我发送整个Address对象列表的子句中,我得到了 ibatis TypeException。如何转换List<Address>为可以发送到更新查询的逗号分隔字符串?

My update query looks like:::

我的更新查询看起来像:::

Update tablename set postcode = #{postCode} where id in #{addressID}.

回答by Kirill Simonov

Using Java 8 you could do it in one line:

使用 Java 8,您可以在一行中完成:

String addressID = addresses
           .stream()
           .map(a -> String.valueOf(a.addressId))
           .collect(Collectors.joining(","));

回答by mohsenJsh

for converting to comma seperated String use something like this:

要转换为逗号分隔的字符串,请使用以下内容:

String commaSeparatedStr = addresses
        .stream()
        .map(c -> String.valueOf(c))
        .collect(Collectors.joining(","));

回答by Ravi

String#join, which accepts delimiter Stringand Iterableelements (which is Listin your case)

String#join,它接受分隔符StringIterable元素(List在你的情况下)

 List<String> strings = new LinkedList<>();
 strings.add("Java");
 strings.add("is");
 strings.add("cool");
 String message = String.join(",", strings); //pass delimiter and List
 //message returned is: "Java,is,cool"

回答by Marit

Iterate over the address objects, retrieve their ID and create a String. Something like this:

遍历地址对象,检索它们的 ID 并创建一个字符串。像这样的东西:

StringBuilder sb = new StringBuilder();

for (Address a: adresses) {
  sb.append(a.getId());
  sb.append(", ");
}

String ids = sb.toString();

回答by Nithin

The format required can be obtained using toString()and replaceAll()methods (using regular Expressions).

可以使用toString()replaceAll()方法(使用正则表达式)获得所需的格式。

String addressString = addresses.toString().replaceAll("[ \[ \] ]", "");

回答by xavierz

List of int- let Java do it:

列表int- 让 Java 来做:

String tmp = addresses.toString();
String csList = tmp.substring(1, tmp.length()-1);

回答by Usman

String.join(", ", addresses) ;

回答by Zico

StringBuilder addressIds = new StringBuilder("");
for(Address address : addresses){
  if (addressIds.length() > 1)  
    addressIds.append(", ");
  addressIds.append("'").append(address.id()).append("'");
}

//in this method you can directly send the string in Update tablename + set postcode + where id in <String>.
methodOfUpdateInQuery(addressIds.toString());