I have Set<String> result
& would like to convert it to comma separated string. My approach would be as shown below, but looking for other opinion as well.
List<String> slist = new ArrayList<String> (result);
StringBuilder rString = new StringBuilder();
Separator sep = new Separator(", ");
//String sep = ", ";
for (String each : slist) {
rString.append(sep).append(each);
}
return rString;
You could count the total length of the string first, and pass it to the StringBuilder constructor. And you do not need to convert the Set first.
From Apache Commons library:
Use:
Another similar question and answer here
The
Separator
you are using is a UI component. You would be better using a simpleString sep = ", "
.