Java Stream findAny() Example

1. Introduction

The findAny() method in Java Streams API provides a means to retrieve any element from a stream that matches a given condition. It is particularly useful in parallel stream operations where the order is not deterministic. findAny() offers a performance advantage in such scenarios because it does not need to process the entire stream if it quickly finds an element that meets the criteria. This method is a valuable tool in concurrent programming and when working with large datasets where efficiency is crucial.

Key Points

1. findAny() returns an Optional to handle cases where no elements match the criteria or the stream is empty.

2. It is a short-circuiting terminal operation, meaning it can terminate the operation based on the internal optimization without necessarily examining all elements.

3. findAny() is best used when the processing order of elements is not important, especially suitable for parallel streams.

2. Program Steps

1. Import necessary Java classes.

2. Create a collection of elements.

3. Convert the collection to a stream, apply a filter, and use findAny() to find an element.

4. Display the result.

3. Code Program

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class StreamFindAnyExample {

    public static void main(String[] args) {
        // Step 2: Create a collection of elements
        List<String> fruits = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");

        // Step 3: Convert to a stream, apply a filter, and use findAny
        Optional<String> anyFruit =
                                          .filter(fruit -> fruit.startsWith("b"))

        // Step 4: Display the result
        if (anyFruit.isPresent()) {
            System.out.println("Found a fruit starting with 'b': " + anyFruit.get());
        } else {
            System.out.println("No fruit found starting with 'b'.");


Found a fruit starting with 'b': banana


1. The program starts by creating a list of fruits. This list acts as the data source for the stream.

2. A stream is created from the list, and a filter is applied to find fruits that start with the letter 'b'. This condition is specified in the lambda expression fruit -> fruit.startsWith("b").

3. The findAny() method is then invoked on the filtered stream. findAny() does not guarantee which element it will return; it selects any element that matches the filter condition. This makes it useful for performance in parallel processing as it can return faster without needing to process all elements.

4. The presence of a result is checked using anyFruit.isPresent(), and if a fruit is found, it is printed out. If no matching fruit is found, an alternate message is displayed.

5. This example highlights how findAny() is employed to efficiently locate an element in a stream without concerning the order, making it particularly effective in scenarios where quick retrieval is more critical than the retrieval order.