abhaygarg12493
abhaygarg12493

Reputation: 1605

Convert a List (long) into List(String ) in Grails2.4.3 (Groovy)

Post call gives data in the form of List<Long> and now I have to convert it into List<String>, so I used this approach for it:

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

Is there any better approach than this?

Upvotes: 7

Views: 9693

Answers (2)

Opal
Opal

Reputation: 84756

Use the Spread Operator

This simple construct should do the job:

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

Upvotes: 9

Paweł Piecyk
Paweł Piecyk

Reputation: 2789

You can use collect:

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

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

Upvotes: 12

Related Questions