get index number of specific key value
get index number of specific key value
I have an ArrayList
:
ArrayList
[doc_count=1439, count=value=689593.0, key=a, doc_count=1439, count=value=405340.0, key=b]
I am trying to use indexOf
or stream
to get the index number of key=a
but I can't.
indexOf
stream
key=a
Is there any idea about this?
Iterate using indices and check the elements. If the element at the current index fits your requirements (i.e. has
key=a
) then return that index. If you reach the end of the array without finding a correct element you don't have an index (which might be indicated by returning -1).– Thomas
Aug 22 at 8:31
key=a
Tell exactly what you're using and provide some of the not working code.
– jle
Aug 22 at 8:32
1 Answer
1
Using java stream to get index number, you can use IntStream.range()
:
IntStream.range()
//Assume your list variable: myList
OptionalInt IntStream.range(0, myList.size())
.filter(i -> "a".equals(myList.get(i).getKey()))
.findFirst();
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Do you have a JSON array? If not, then can you show us exactly what is stored in your array list?
– Tim Biegeleisen
Aug 22 at 8:29