Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8: How to get element at an index from a Stream

I have a stream, I need to get the element at an index in this stream Basically, a fetch on the basis of index of element like in List list.get(index)

like image 875
Arpit Avatar asked Dec 03 '25 13:12

Arpit


1 Answers

There is a skip method that abandons access to first N elements https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#skip-long-

After that you can use limit(1) and you have just one element at specified index.

EDIT: After @Holger answer there's also a possibility to call

stream().skip(index).findFirst()
like image 107
Mariusz Lotko Avatar answered Dec 07 '25 06:12

Mariusz Lotko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!