Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy Converting List of objects to Comma Separated Strings

Tags:

groovy

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.

like image 322
RanPaul Avatar asked Mar 25 '14 22:03

RanPaul


People also ask

How do you convert a list to a comma separated string?

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.

How do I convert a list to a string in Groovy?

In Groovy we can use the toListString() method to get a String representation for a given collection. And since Groovy 1.7.

How to use each () method in Groovy?

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 }

How to sort a list in Groovy using comparator?

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])

What are the different types of string representations in Groovy?

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.

How do you iterate over a list in Groovy?

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}


2 Answers

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(", ")
like image 194
Seagull Avatar answered Sep 29 '22 11:09

Seagull


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
}
like image 39
tim_yates Avatar answered Sep 29 '22 10:09

tim_yates