During my work with databases, I noticed that I write query strings and in these strings, I have to put several restrictions in the where-clause from a list/array/collection. Should look like this:
select * from customer
where customer.id in (34, 26, ..., 2);
You can simplify this by reducing this to the question that you have a collection of strings and want to create a comma-separated list of these strings in just one string.
The approach I have used so far is something like that:
String result = "";
boolean first = true;
for(String string : collectionOfStrings) {
if(first) {
result+=string;
first=false;
} else {
result+=","+string;
}
}
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.
What is your (more) elegant way?