Convert list (long) to list (string) in Grails2.4.3 (Groovy)

The post call gives the data in the form List<Long>

and now I have to convert it to List<String>

, so I used this approach for that:

deleteAvailablity.startDate.each {
   startDateList.add(it.toString())
}
deleteAvailablity.startDate = startDateList

      

Is there a better approach than this one?

+3


source to share


2 answers


You can use collect

:



def listOfLongs = [0L, 1L, 2L]
def listOfStrings = listOfLongs.collect { it.toString() }

assert listOfStrings == ["0", "1", "2"]

      

+5


source


Use the Spread Operator

This simple construct should do the following:



assert [1l, 2l, 3l]*.toString() == ['1', '2', '3']

      

+7


source







All Articles