Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy reverse map keys

Tags:

map

groovy

I would like to get last X entries of a map.

If i would like to get first entries it can be done fairly easy in groovy: map.take(10) gets me first 10 entries of a map. But how to get LAST 10 entries? there is no map.reverse() method.

like image 271
Pma Avatar asked Oct 07 '22 16:10

Pma


1 Answers

You could use drop like so:

map.drop( map.size() - 10 )

To drop all but the last 10 elements

An alternative would be to use the iterator, which can be reversed:

map.iterator().reverse().take( 10 ).reverse().collect()

But it's much messier, and uses more resources

like image 113
tim_yates Avatar answered Oct 10 '22 02:10

tim_yates