Getting values ​​from nested lists in groovy

I have a 3 level nested list in groovy, like this

rList = [[[12, name1],[22,name2],[49,name3]],[[33, name5],[22,name6],[21, name7]]] 

how can I repeat it to get the values ​​of the names from the 1st sublist so I want rsublist = [name1, name2, name3]

Thanks in advance.

+6
source share
2 answers
 rList = [[[12, 'name1'], [22, 'name2'], [49, 'name3']], [[33, 'name5'], [22, 'name6'], [21, 'name7']]] names = rList[0]*.getAt(1) assert names == ['name1', 'name2', 'name3'] 

First, rList[0] provides you with the first sub-list, which is [[12, name1], [22, name2], [49, name3]] . Then the distribution operator , *. used to apply the same getAt(1) method to every element of this list, which will return a list with every second element of the subscriptions, which are the values ​​you were looking for :)

You can also use rList[0].collect { it[1] } , which is equivalent and might be more familiar if you do not use the distribution operator.

+7
source

According to @epidemian's answer here is a more readable version:

 rList.first()*.last() 
+2
source

Source: https://habr.com/ru/post/917708/


All Articles