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?





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





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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)