Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to do this in Groovy?

Tags:

groovy

I want a code that will return a list like this :

def list = ["RR","SS"]
//code to get the output as [R,R,S,S]

I came up with idea like this:

def Ash = ["RR","as","RTY"]
def listA = []
for(i=0;i<Ash.size();i++)
{ 
    listA <<  Ash[i].collect{ it as String }
}
AshNew = listA.flatten()
println AshNew // this prints [R, R, a, s, R, T, Y] which is what i needed..

But still i want to know that whether we can do this similar stuff in Groovy by using another way? Since I'm a novice in Groovy i want to know more Groovier solution! Thanks for your answer!

like image 761
Ant's Avatar asked Feb 23 '23 14:02

Ant's


1 Answers

Is

AshNew = Ash.collect { it as List }.flatten()

any better?

like image 106
tim_yates Avatar answered Mar 16 '23 13:03

tim_yates