I have a collection of Ingredient
objects for which I'd like get all their names (via getName()
) and join them into a comma-delimited string. Currently my code looks like this:
public static String getIngredientList(Collection<Ingredient> ingredients) {
final Iterator<Ingredient> iterator = ingredients.iterator();
final String[] names = new String[ingredients.size()];
for (int i = 0; iterator.hasNext(); i++) {
names[i] = iterator.next().getName();
}
return TextUtils.join(", ", names);
}
I'm wondering if there's a more concise way to collect all the names into a String[]
object. If this were Ruby, for example, it'd be easy to pull off a short one-liner to do exactly what I need:
ingredients.map(&:name).join(', ')