akki akki - 6 months ago 22
Android Question

How to get particular index for any data from list using Java Stream?

Here is a code to check if particular string is matched or not from List

if (Stream.of(getPlayingSongList()).filter(o -> o.getId().equals(songList.get(position).getId())).findFirst().isPresent()) {
ToastMethod.show(getActivity(), "Song is already in queue", true);
}


I want to know, which index from
getPlayingSongList()
is matching.

I am using
com.annimon:stream
library in android.

Answer

What about using IntStream.range ?

Since I don't have you whole code, I'm just putting a dummy example :

String[] myArray = { "a", "b", "c", "d" };
OptionalInt c = IntStream.range(0, myArray.length).filter(n -> "c".equals(myArray[n])).findFirst();

if (c.isPresent()) {
   System.out.println(c.getAsInt());
}

>>>> 2