<p>During my work with databases I noticed that I write query strings and in this strings I have to put several restrictions in the where-clause from a list/array/collection. Should look like this:</p>

<pre><code>select * from customer 
where customer.id in (34, 26, ..., 2);
</code></pre>

<p>You can simplify this by reducing this to the question that you have collection of strings and want to create a comma-separated list of this strings in just one string.</p>

<p>My approach I have used so far is something like that:</p>

<pre><code>String result = "";
boolean first = true;
for(String string : collectionOfStrings) {
    if(first) {
        result+=string;
        first=false;
    } else {
        result+=","+string;
    }
}
</code></pre>

<p>But this is as you can see very ugly. You cannot see what happens there on the first look, especially when the constructed strings (like every SQL query) is getting complicated.</p>

<p>What is your (more) elegant way?</p>

I'm not sure how "sophisticated" this is, but it's certainly a bit shorter. It will work with various different types of collection e.g. Set<Integer>, List<String>, etc.
```
```
public static final String toSqlList(Collection<?> values) {

    String collectionString = values.toString();

    // Convert the square brackets produced by Collection.toString() to round brackets used by SQL
    return "(" + collectionString.substring(1, collectionString.length() - 1) + ")";
}
```
```
Exercise for reader
: modify this method so that it correctly handles a null/empty collection :)