[英]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. 我想知道,
getPlayingSongList()
哪个索引匹配。
I am using com.annimon:stream
library in android. 我在android中使用
com.annimon:stream
库。
What about using IntStream.range
? 使用
IntStream.range
怎么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
using Akah's suggestion, for android, below code works well. 使用Akah的建议,对于Android,下面的代码效果很好。
Optional<Integer> c = Stream.range(0, getPlayingSongList().size()).filter(n -> songList.get(position).getId().equals(getPlayingSongList().get(n).getId())).findFirst();
if (c.isPresent()) {
Log.i("get index", c.get()+" ok");
}
Thanks a lot. 非常感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.