How to collect indices conditionally in jdk8

Sometimes, I use API that requires indices as parameters. It was the only case when I used for loop with indices instead of Stream API. Here is a way to get indices only with Stream API
    // just initialisation
    List<String> listOfStrings = Stream.of("Dan", "Han", "Gun", "Sun", "Dnepropetrovsk").collect(Collectors.toList());

    // getting indices
    int[] indices = IntStream.range(0, listOfStrings.size()).filter(i -> listOfStrings.get(i).startsWith("D")).toArray();
Of course, for LinkedList this should work slowly than for loop.

One thought on “How to collect indices conditionally in jdk8”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>