Groovy Converting List of objects to Comma Separated Strings
If you don't want to create a new list (which you say you don't want to do), you can use inject
currenciesList.inject( '' ) { s, v ->
s + ( s ? ', ' : '' ) + v
}
Try currenciesList.code.join(", ")
. It will create list at background, but it's minimal code solution.
Also do you know, that your code may be even Groovier? Look at Canonical or TupleConstructor transformations.
//This transform adds you positional constructor.
@groovy.transform.Canonical
class CurrencyType {
int id
String code
String currency
}
def currenciesList = [
new CurrencyType(1,"INR", "Indian Rupee"),
new CurrencyType(1,"USD", "US Dollar"),
new CurrencyType(1,"CAD", "Canadian Dollar")
]
//Spread operation is implicit, below statement is same as
//currenciesList*.code.join(/, /)
currenciesList.code.join(", ")