Solved

How to convert a string to list in Groovy

Posted on 2009-04-13
4
2,931 Views
Last Modified: 2013-11-13
Hi experts,
  I have a requirement to convert a "list-like" string into a real list in Groovy. For example, if I have a string as '[1,2,3,4,5]', how can I write a closure to convert it into a list type as [1,2,3,4,5]? If possible, could you please show me the real code?
  I'm totally new to Groovy and learning it right now.
  Thanks!

Cheers,
Michael
0
Comment
Question by:michaelzhou23
  • 2
4 Comments
 
LVL 10

Expert Comment

by:mahome
ID: 24135486
There are likely many of possibilities in groovy. Here is one of them:

// Closure
def convertToList = { it[1..-2].split(',').collect{ it } }
 
// Example-Usage
def string = "[1,2,3,4,5]"
def result = convertToList(string)
 
println result
println result.getClass()
        

Open in new window

0
 
LVL 35

Expert Comment

by:TimYates
ID: 24140212
This will return them as ints...

But as mahome says, there's loads of ways round this
def convertInts = { list ->
  list.tokenize( "[, ]" ).collect { it as Integer }  
}
def iList = convertInts( "[1, 2, 3, 4, 5, 11]" )
iList.each { println "$it (${it.class})" }

Open in new window

0
 

Author Comment

by:michaelzhou23
ID: 24143634
Hi Thanks a lot!
I tested them and they worked. But I was wondering whether there is any way that I can put the whole things in one statement? I mean i would need this in a groovy expression.
Thanks!
0
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 24145113
?
"[1,2,3,4,5]".tokenize( "[, ]" ).collect { it as Integer }

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
wild fly 8 startup error 2 41
JDBC performance 8 29
Github api to fetch the number of collaborators in a git repo 1 29
Java List 4 40
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question