Java 8 streams findAny() and findFirst()

Posted in Java By Raj On October 29, 2017

 

In this article, I am going to showcase you how to use Java 8 stream findAny() and findFirst(), first create a user class something like below 

Step 2: create user collection

Step 3: Java Stream findFirst()

The below code first applies filter which is user role equal to author, once it returns stream object then it gets the first object

Step 3: Java Stream findAny()

The below code first applies filter which is user role equal to Contributor, once it returns stream object then it gets any matching object, this is something like contains method in the ArrayList

Final code and output

Output