Java Stream findFirst(), findAny() With Examples

In the Java Stream API there are two methods findFirst() and findAny() where-

  • findFirst() returns the first element of the Stream.
  • findAny() returns any element of the Stream.

Java Stream findFirst() method

The findFirst() method returns an Optional describing the first element of this stream in case Stream is not empty, returns an empty Optional if the stream is empty.

Optional<T> findFirst()

findFirst() is a short-circuiting terminal operation which means it may terminate in finite time when presented with infinite input.

Java Stream findFirst() examples

1. A simple example to get first element of the list where List is the source for the Stream.

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

public class FindFirstDemo {

  public static void main(String[] args) {
    List<Integer> list = Arrays.asList(6, 8, 5, 6, 7, 8);
    Optional<Integer> firstElem = list.stream().findFirst();
    if(firstElem.isPresent()) {
      System.out.println("First Element- " + firstElem.get());
    }else {
      System.out.println("No element found");
    }
  }
}

Output

First Element- 6

2. Using findFirst() in combination with other Stream method like filter to get the first element in the Stream which is greater than 6.

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

public class FindFirstDemo {

  public static void main(String[] args) {
    List<Integer> list = Arrays.asList(6, 8, 5, 6, 7, 8);
    list.stream()
        .filter(n -> n > 6)
        .findFirst()
        .ifPresent(System.out::println);
  }
}

Output

8

Java Stream findAny() method

The findAny() method returns an Optional describing some element of the stream in case Stream is not empty, returns an empty Optional if the stream is empty.

Optional<T> findAny()

findAny() is a short-circuiting terminal operation which means it may terminate in finite time when presented with infinite input.

The behavior of this operation is explicitly nondeterministic; it is free to select any element in the stream.

Java Stream findAny() examples

1. A simple example to get any element of the list where List is the source for the Stream. Though most of the time findAny() also returns the first element but this behavior is not guaranteed.

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

public class FindAnyDemo {
  public static void main(String[] args) {
    List<Integer> list = Arrays.asList(6, 8, 5, 6, 7, 8);
    Optional<Integer> listElem = list.stream().findAny();
    if(listElem.isPresent()) {
      System.out.println("List Element- " + listElem.get());
    }else {
      System.out.println("No element found");
    }
  }
}

Output

List Element- 6

2. Using findAny() in combination with other Stream method like filter to get the first element in the Stream which is greater than 5.

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

public class FindAnyDemo {
  public static void main(String[] args) {
    List<Integer> list = Arrays.asList(6, 8, 5, 6, 7, 8);
    list.stream()
        .filter(n -> n > 5)
        .findAny()
        .ifPresent(System.out::println);
 
  }
}

Output

6

Related Posts

That’s all for the topic Java Stream findFirst(), findAny() With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

Share this post:

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.