Course Content
Stream API
Stream API
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:
Main
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:
Main
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?
Thanks for your feedback!