Mark
Mark

Reputation: 18787

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?

Upvotes: 8

Views: 7622

Answers (1)

Mark
Mark

Reputation: 18787

I have found the solution:

println numbers.findIndexValues {
    it == 4;
}

Groovy is amazing!!!

Upvotes: 14

Related Questions