
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 powerful tool for fetching an arbitrary element from a stream. It provides a quick and efficient way to retrieve any element without specifying a particular condition. This method returns an Optional object, a container that may or may not contain a non-null value.
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 return the first encountered element.
Syntax
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 steam from the list.
- The findAny() method fetches any element from the stream. Since this is a sequential stream, it returns the first element (10).
Optionalres = list.stream().findAny();
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<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");
findAny() is checked with isPresent() to determine if an element was retrieved
if (res.isPresent()) {}
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!"); } } }
Output
Kevin
Conclusion
The findAny() method is a flexible and efficient way to retrieve an element from a Java Stream. Whether you're working with sequential or parallel streams, it offers a quick way to get an arbitrary element. By understanding its behavior and use cases, you can leverage this method to simplify your code and improve performance.