Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map list of tuples into list of second elements in Groovy?

Tags:

tuples

groovy

I have:

List<Tuple2<String, String>> listOfTuples

I would like to have a list of second elements from each Tuple2

Eg.

[Tuple2('1','foo'), Tuple2('2','bar')]

to

['foo','bar']
like image 305
pixel Avatar asked Sep 14 '25 08:09

pixel


1 Answers

It's easy. You can use Groovy' spread operator to extract a list of the tuple's second elements.

Consider the following example:

def list = [new Tuple2('1','foo'), new Tuple2('2','bar')]

assert ['foo', 'bar'] == list*.second
like image 126
Szymon Stepniak Avatar answered Sep 16 '25 21:09

Szymon Stepniak