I\'ve run into a common situation where i have a list of objects and need to generate a comma separated string with a single property, which are then each surrounded by sing
Stream.map() and Collectors.joining() are your friends here.
companies.stream()
.map(Company::getId)
.map(s -> "'" + s + "'")
.collect(joining(","));
You can create a helper method, but in my judgement the above is succinct enough that it isn't worthwhile:
static String mapAndJoin(Collection c, Function f){
return c.stream()
.map(f)
.map(s -> "'" + s + "'")
.collect(joining(","));
}
mapAndJoin(companies, Company::getId);