Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Retrieving Elements from a Stream with findFirst() and findAny() | Terminal Operations in the Stream API
Stream API
course content

Course Content

Stream API

Stream API

1. Fundamentals and Functional Capabilities of Stream API
4. Practical Applications of Stream API

book
Retrieving Elements from a Stream with findFirst() and findAny()

In Stream API, you can also extract just one matching element from the entire stream.

For example, in an online store, you might look for the first discounted product or any item in stock. In such cases, the findFirst and findAny methods are useful because they allow you to quickly extract an element from a data stream.

The findFirst() Method

The findFirst() method is used to retrieve the first element in a stream. This is especially useful when the order of elements matters, such as when working with an ordered stream.

This method returns an Optional<T>, meaning the element may or may not be found.

Example Usage

In an online store, you need to find the first discounted product and print its name:

java

Main

copy
12345678910111213141516171819202122232425262728293031323334353637383940
package com.example; import java.util.List; import java.util.Optional; import java.util.Arrays; public class Main { public static void main(String[] args) { List<Product> products = Arrays.asList( new Product("Laptop", false), new Product("Smartphone", true), new Product("Tablet", true) ); Optional<Product> discountedProduct = products.parallelStream() .filter(Product::hasDiscount) .findFirst(); discountedProduct.ifPresent(product -> System.out.println("Discounted product: " + product.getName())); } } class Product { private String name; private boolean discount; public Product(String name, boolean discount) { this.name = name; this.discount = discount; } public boolean hasDiscount() { return discount; } public String getName() { return name; } }

The code creates a list of Product objects, each with a discount field. The filter(Product::hasDiscount) method selects only discounted products, and findFirst() retrieves the first one. If a product is found, its name is printed.

The findAny() Method

The findAny() method retrieves any element from a stream. It is particularly useful in parallel processing since it can return the first available element, making it faster than findFirst() in some cases.

Like findFirst(), this method returns an Optional<T>, helping to avoid issues with null.

Example Usage

Let's find any available product and print its name:

java

Main

copy
123456789101112131415161718192021222324252627282930313233343536373839404142
package com.example; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Stream; public class Main { public static void main(String[] args) { List<Product> products = Arrays.asList( new Product("Laptop", false), new Product("Smartphone", true), new Product("Tablet", true) ); Optional<Product> availableProduct = products.parallelStream() .filter(Product::hasDiscount) .findAny(); availableProduct.ifPresent(product -> System.out.println("Product found: " + product.getName())); } } class Product { private String name; private boolean discount; public Product(String name, boolean discount) { this.name = name; this.discount = discount; } public boolean hasDiscount() { return discount; } public String getName() { return name; } }

This code creates a products list containing Product objects with a discount field. The filter(Product::hasDiscount) method filters out only discounted products, and findAny() selects a random one. If a product is found, its name is printed.

1. What does the findFirst() method return in Stream API?

2. What is the main difference between findAny() and findFirst()?

3. Which method is better for performance in parallel streams?

question mark

What does the findFirst() method return in Stream API?

Select the correct answer

question mark

What is the main difference between findAny() and findFirst()?

Select the correct answer

question mark

Which method is better for performance in parallel streams?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 10
We're sorry to hear that something went wrong. What happened?
some-alt