Java Stream findAny() with Examples



In this article, we will learn the findAny() method with examples in Java. The findAny() method in Java Streams is a tool that is used for fetching an arbitrary element from a stream. It provides a quick and easy way to retrieve any element without writing any conditions.

This method returns a container that may or may not contain a non-null value, which is called an Optional object.

What is the findAny() Method?

The findAny() method retrieves any element from the stream, and the result is wrapped in an Optional object. If the stream is empty, the Optional object will be empty as well.

  • Returns: An Optional containing an element from the stream or an empty Optional if the stream is empty.
  • Behavior: For parallel streams, it may return any element from the stream. For sequential streams, it generally returns the first encountered element.

Syntax

Following is the syntax of the findAny() method -

Optional<T> findAny();

Where T is the type of elements in the stream.

Using findAny() with a List of Integers

The findAny() method can be applied to a list of integers to retrieve an arbitrary value from the stream.

  • The list.stream() creates a sequential stream from the list.
  • The findAny() method fetches any element from the stream. Since this is a sequential stream, it returns the first element (10).
Optional<Integer> res = list.stream().findAny();

Below is an example of implementing the findAny() method in Java -

import java.util.*;
public class Demo {
   public static void main(String[] args){
      List<Integer> list = Arrays.asList(10, 20, 30, 40, 50);
      Optional<Integer> res = list.stream().findAny();
      if (res.isPresent()) {
         System.out.println(res.get());
      } else {
         System.out.println("None!");
      }
   }
}

Output

10

Using findAny() with a List of Strings

The findAny() method is equally effective for non-numeric collections, such as a list of strings. It allows you to retrieve any element from the stream without requiring any specific condition. The myList.stream() creates a stream from the list of strings -

List myList = Arrays.asList("Kevin", "Jofra","Tom", "Chris", "Liam");

Example

Below is an example of implementing the findAny() method in Java -

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      List<String> myList = Arrays.asList("Kevin", "Jofra","Tom", "Chris", "Liam");
      Optional<String> res = myList.stream().findAny();
      if (res.isPresent()) {
         System.out.println(res.get());
      } else {
         System.out.println("None!");
      }
   }
}

The output of the above code will be -

Kevin

Using findAny() with a List of Custom Objects

The findAny() method can also be applied to a list of custom objects. In this case, we will create a class called Person with fields for name and age. We will create a list of Person objects and use the findAny() method to retrieve any element from the stream.

  • The list.stream() creates a sequential stream from the list.
  • The findAny() method fetches any element from the stream. Since this is a sequential stream, it returns the first element (Person object).

Example

Below is an example of implementing the findAny() method in Java -

import java.util.*;
public class Person {
   String name;
   int age;
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }
   public String getName() {
      return name;
   }
   public int getAge() {
      return age;
   }
}

public class Demo {
   public static void main(String[] args) {
      List<Person> list = Arrays.asList(new Person("Kevin", 25), new Person("Jofra", 30), new Person("Tom", 35));
      Optional<Person> res = list.stream().findAny();
      if (res.isPresent()) {
         System.out.println(res.get().getName() + " " + res.get().getAge());
      } else {
         System.out.println("None!");
      }
   }
}

The output of the above code will be -

Kevin 25
Updated on: 2025-05-09T18:39:31+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements