Java Stream findAny() Example

 In this tutorial, we will learn Java 8 Stream findAny() terminal operation with an example.

The Java Stream findAny() method can find a single element from the Stream. The element found can be from anywhere in the Stream. There is no guarantee about from where in the stream the element is taken. 

Java Stream findAny() Example

Here is a Java Stream findAny() example:

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<>();

        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("one");

        Stream<String> stream = stringList.stream();

        Optional<String> anyElement = stream.findAny();

        System.out.println(anyElement.get());

    }
}

Output:

one

Java Stream Methods/APIs Examples