Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index of element that matches a predicate [duplicate]

I know how to find the first element of a list by predicate: Find first element by predicate

Is there an easy way to get the index of that element?

like image 752
Roland Avatar asked Dec 14 '22 23:12

Roland


1 Answers

If I understood correctly, that's the classic case where you need IntStream; but that would only apply for a List obviously.

IntStream.range(0, yourList.size())
   .filter(i -> yourList.get(i)... your filter condition)
   .collect(Collectors.toList());
like image 132
Eugene Avatar answered Jan 15 '23 11:01

Eugene