ZetCode

Java Stream findFirst/findAny

last modified January 27, 2024

In this article we shows how to find first or any element in Java streams.

Java Stream

Java Stream is a sequence of elements from a source that supports aggregate operations. Streams do not store elements; the elements are computed on demand. Elements are consumed from data sources such as collections, arrays, or I/O resources.

The findFirst method returns an Optional describing the first element of a stream, or an empty Optional if the stream is empty.

The findAny method returns an Optional describing some element of a stream, or an empty Optional if the stream is empty.

Java Stream findFirst example

In the next example we use the findFirst method.

com/zetcode/FindFirstEx.java
package com.zetcode;

import java.util.List;

public class FindFirstEx {

    public static void main(String[] args) {

        var words = List.of("war", "cup", "cloud", "alert", "be", "ocean", "book");
        var empty = List.of();

        var first = words.stream().findFirst().orElse("not found");
        System.out.println(first);

        var first2 = empty.stream().findFirst().orElse("not found");
        System.out.println(first2);
    }
}

We find first elements of the list of words.

var words = List.of("war", "cup", "cloud", "alert", "be", "ocean", "book");
var empty = List.of();

We have two lists of strings. One has seven words, the other is empty.

var first = words.stream().findFirst().orElse("not found");

We find the first element of the list. If no element is found, we return "not found" string.

war
not found

In the second example, we filter a list of words and then find its first matching element.

com/zetcode/FindFirstEx2.java
package com.zetcode;

import java.util.List;

public class FindFirstEx2 {

    public static void main(String[] args) {

        var words = List.of("war", "cup", "cloud", "alert", "be",
                "water", "warm", "ocean", "book");

        var first = words.stream().filter(e -> e.startsWith("w"))
                .findFirst().orElse("not found");
        System.out.println(first);
    }
}

In the example, we find the first word that starts with "w".

war

Java Stream findAny example

In the next example, we use the findAny method.

com/zetcode/FindAnyEx.java
package com.zetcode;

import java.util.List;

public class FindAnyEx {

    public static void main(String[] args) {

        var words = List.of(
                new User("John Doe", "gardener"),
                new User("Roger Roe", "driver"),
                new User("Jozef Kral", "shopkeeper"),
                new User("Boris Brezov", "musician"),
                new User("Lucia Novak", "teacher"));

        var res = words.stream().filter(u -> u.occupation().equals("gardener"))
                .findAny();

        res.ifPresent(System.out::println);
    }
}

record User(String name, String occupation) {
}

We have a list of users. We find out if there is any user who is a gardener.

User[name=Roger Roe, occupation=driver]

Source

Java Stream documentation

In this article we have have presented Java Stream findFirst and findAny methods.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Java tutorials.