Java Stream findAny() with examples

 162 total views,  1 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream findAny() with examples.

findAny() is a part of the Stream pipeline and terminal operation. It belongs to the Stream interface.

findAny() allows us to get any element of the stream. Once findAny() is invoked on a stream, it returns any element of the stream and the stream will be closed. We cannot reuse the stream as we have invoked a terminal operation i.e., findAny().

If the stream is empty, findAny() will return an empty optional.

Note: In most cases, findAny() will return the first element of the stream but it does not guarantee it. So better use findFirst() if the first element is needed.

Method Description

Optional<T> findAny()

Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.

Let us now see a few examples of Java 8 stream findAny()

Java stream findAny() operation example

public class StaticReference
{
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(3);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        Optional<Integer> result = list.stream().findAny();
        System.out.println("findAny Integer : " + result.get());
    }
}

Output

findAny Integer : 2

Java stream findAny() operation example – String

public class StaticReference
{
    public static void main(String[] args)
    {
        List<String> listString = new ArrayList<>();
        listString.add("q");
        listString.add("b");
        listString.add("c");
        listString.add("d");
        Optional<String> output = listString.stream().findAny();
        System.out.println("findAny String : " + output.get());
    }
}

Output

findAny String : q

Java stream findAny() operation empty stream example

public class StaticReference
{
    public static void main(String[] args)
    {
        List<String> listString = new ArrayList<>();
        Optional<String> output = listString.stream().findAny();
        if (output.isPresent())
        {
            System.out.println("findAny String : " + output.get());
        }
        else
        {
            System.out.println("Stream is empty");
        }
    }
}

Output

Stream is empty

Conclusion

findAny() operation is a terminal operation and it is used for getting any element in the stream. Although most of the time, findAny() returns the first element it is not guaranteed

References : Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.