HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 8 / Java Stream findAny()

Java Stream findAny()

The findAny() method returns an Optional describing the any element of the given stream if Stream is non-empty, or an empty Optional if the stream is empty.

In non-parallel streams, findAny() will return the first element in most of the cases but this behavior is not gauranteed.

The Stream.findAny() method has been introduced for performance gain in case of parallel streams, only.

1. Stream findAny() Method

Optional<T> findAny()
  • The findAny() method is a terminal short-circuiting operation.
  • The findAny() method returns an Optional.
  • The Optional contains the value as any element of the given stream, if Stream is non-empty. The returned element is the first element in most of the cases.
  • The Optional contains the empty value, if Stream is empty.
  • If the element selected is null, NullPointerException is thrown.
  • For all the sequential and parallel streams, it may return any element. The behavior of findAny() does not change by the parallelism of the Stream.
  • Similarily, there is no gauranteed behavioral difference in case of a strream has defined encounter order or it has no encounter order at all.

2. Stream findAny() Example

In the given example, we are using the finaAny() method to get any element from the Stream. As soon as, we get the first element, the stream operation moves to ifPresent() method.

We print the recieved element in using the method reference inside ifPresent() method.

import java.util.stream.Stream;

public class Main 
{
	public static void main(String[] args) 
	{
		//sequential stream

		Stream.of("one", "two", "three", "four")
				.findAny()
				.ifPresent(System.out::println);
		
		//parallel stream

		Stream.of("one", "two", "three", "four")
			.parallel()
			.findAny()
			.ifPresent(System.out::println);
	}
}

Program output.

one
one

3. Stream findFirst() vs findAny()

In non-parallel streams, findFirst() and findAny(), both may return the first element of the Stream in most cases. But findAny() does not offer any guarantee of this behavior.

Use findAny() to get any element from any parallel stream in faster time. Else we can always use findFirst() in most of the cases.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java 8 Tutorial

  • Java 8 Features
  • Java 8 forEach
  • Java 8 Stream
  • Java 8 Boxed Stream
  • Java 8 Lambda Expression
  • Java 8 Functional Interface
  • Java 8 Method Reference
  • Java 8 Default Method
  • Java 8 Optional
  • Java 8 Predicate
  • Java 8 Regex as Predicate
  • Java 8 Date Time
  • Java 8 Iterate Directory
  • Java 8 Read File
  • Java 8 Write to File
  • Java 8 WatchService
  • Java 8 String to Date
  • Java 8 Difference Between Dates
  • Java 8 Join Array
  • Java 8 Join String
  • Java 8 Exact Arithmetic
  • Java 8 Comparator
  • Java 8 Base64
  • Java 8 SecureRandom
  • Internal vs External Iteration

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)