Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all indices of an element in a Groovy list

I want to find positions of all elements of array with specific value. Example:

def numbers = [1, 2, 3, 4, 5, 6, 5, 4, 6, 4, 9, 2];

I want to find positions of 4, here position numbers are 3,7,9

How to find it elegant with nice groovy collections methods?

like image 304
Mark Avatar asked Jan 26 '14 20:01

Mark


1 Answers

I have found the solution:

println numbers.findIndexValues {
    it == 4;
}

Groovy is amazing!!!

like image 125
Mark Avatar answered Oct 31 '22 16:10

Mark