How to convert List<Object> into comma separated String
Asked Answered
C

9

35

I am getting list of Address objects from the DB call.

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

Each Address has an int addressId property.

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

My update query looks like:::

Update tablename set postcode = #{postCode} where id in #{addressID}.
Cougar answered 4/2, 2018 at 5:43 Comment(5)
Consider this approach again. In particular, read about SQL injection.Efface
@Efface Converting into string and sending in IN clause will not solve the problem?I dont know how to pass list in update query..Cougar
It will solve this problem but create a new one (a security vulnerability).Efface
@Efface Creating a new one means.Sorry i did not understood..Cougar
I don't see how SQL injection is possible in this case. There is no user input that can affect the creation of the addressID string in any way, and addressId field has an integer type.Bush
B
61

Using Java 8 you could do it in one line:

String addressID = addresses
           .stream()
           .map(a -> String.valueOf(a.addressId))
           .collect(Collectors.joining(","));
Bush answered 4/2, 2018 at 5:51 Comment(2)
a non "List<string>" example!! Thank you !Sciomachy
and if addresses is null?Slavey
S
16

for converting to comma seperated String use something like this:

String commaSeparatedStr = addresses
        .stream()
        .map(String::valueOf)
        .collect(Collectors.joining(","));
Setup answered 4/2, 2018 at 5:51 Comment(1)
In case someone need a comma separated of variable inside Address object, use the getter inside map .map(c -> String.valueOf(c.getName()))Primary
G
8

String#join, which accepts delimiter String and Iterable elements (which is List in your case)

 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"
Giraudoux answered 4/2, 2018 at 5:58 Comment(0)
E
3

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

StringBuilder sb = new StringBuilder();

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

String ids = sb.toString();
Elana answered 4/2, 2018 at 5:51 Comment(0)
S
3
String.join(", ", addresses) ;
Sistrunk answered 8/5, 2020 at 4:52 Comment(1)
Super simple. Super good.Adjoining
A
1
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());
Affiance answered 4/2, 2018 at 5:51 Comment(0)
W
1

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

String addressString = addresses.toString().replaceAll("[ \\[ \\] ]", "");
Weeny answered 4/2, 2018 at 7:4 Comment(0)
D
1

List of int - let Java do it:

String tmp = addresses.toString();
String csList = tmp.substring(1, tmp.length()-1);
Deragon answered 10/9, 2019 at 21:42 Comment(0)
B
0

21 June Update:

Using the below code snippet can be used to generate a comma separated string

String finalString = list.map((item) => item).join(', ');
Barrera answered 21/6 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.