Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Retrieving Elements from a Stream with findFirst() and findAny() | Section
Stream API in Java

bookRetrieving 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.

Optional<T> findFirst();

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.java

Main.java

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.

Optional<T> findAny();

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.java

Main.java

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?

正しい答えを選んでください

question mark

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

正しい答えを選んでください

question mark

Which method is better for performance in parallel streams?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  36

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  36
some-alt