I have a groovy list of CurrencyTypes example
class CurrencyType
{
int id;
def code;
def currency;
CurrencyType(int _id, String _code, String _currency)
{
id = _id
code = _code
currency = _currency
}
}
def currenciesList = new ArrayList<CurrencyType>()
currenciesList.add(new CurrencyType(1,"INR", "Indian Rupee"))
currenciesList.add(new CurrencyType(1,"USD", "US Dollar"))
currenciesList.add(new CurrencyType(1,"CAD", "Canadian Dollar"))
I want the list of codes as comma separated values like INR, USD, CAD with minimal code and with out creating new list.
Approach: This can be achieved with the help of join() method of String as follows. Get the List of String. Form a comma separated String from the List of String using join() method by passing comma ', ' and the list as parameters. Print the String.
In Groovy we can use the toListString() method to get a String representation for a given collection. And since Groovy 1.7.
The each () method accepts a closure and is very similar to the foreach () method in Java. Groovy passes an implicit parameter it which corresponds to the current element in each iteration: def list = [ 1, "App", 3, 4 ] list.each {println it * 2 }
By default, Groovy sorts the items in a list based on their natural ordering: assertTrue([1,2,1,0].sort() == [0,1,1,2]) But we can also pass a Comparator with custom sorting logic: Comparator mc = {a,b -> a == b? 0: a < b? 1 : -1} def list = [1,2,1,0] list.sort(mc) assertTrue(list == [2,1,1,0])
So, there are four main types of string representations that are instantiated either through java.lang.String or groovy.lang.GString in Groovy — single-quoted string, double-quoted string, GString,and triple single-quoted string. Let's talk about them with some examples: Let's first see the single-quoted string.
Lets start by looking at the two methods for iterating over a list. The each() method accepts a closure and is very similar to the foreach() method in Java. Groovy passes an implicit parameter it which corresponds to the current element in each iteration: def list = [1,"App",3,4] list.each {println it * 2}
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(", ")
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With