Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to find the first element in Stream in Java 8? findFirst() Example

In Java 8, you can use the Stream.findFirst() method to get the first element of Stream in Java. This is a terminal operation and is often used after applying several intermediate operations e.g. filter, mapping, flattening, etc. For example, if you have a List of String and you want to find the first String whose length is greater than 10, you can use the findFirst() method along with stream() and filter() to get that String. The stream() method gets the Stream from a List, which then allows you to apply several useful methods defined in the java.util.Stream class like filter(), map(), flatMap() etc.


This post first appeared on Javarevisited: Blog About Java Programmin, please read the originial post: here

Share the post

How to find the first element in Stream in Java 8? findFirst() Example

×

Subscribe to Javarevisited: Blog About Java Programmin

Get updates delivered right to your inbox!

Thank you for your subscription

×