- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
LongStream findAny() method in Java
The findAny() method of the LongStream class in Java returns an OptionalLong describing some element of the stream, or an empty OptionalLong if the stream is empty.
The syntax is as follows:
OptionalLong findAny()
Here, OptionalLong is a container object which may or may not contain a long value.
To use the LongStream class in Java, import the following package:
import java.util.stream.LongStream;
The following is an example to implement LongStream findAny() method. The isPresent() method of the OptionalLong class returns true if the value is present:
Example
import java.util.OptionalLong; import java.util.stream.LongStream; public class Demo { public static void main(String[] args) { LongStream longStream = LongStream.of(50000L, 70000L, 85000L, 90000L, 95000L).parallel(); longStream = longStream.filter(a → a > 75000L); OptionalLong res = longStream.findAny(); if (res.isPresent()) { System.out.println(res.getAsLong()); } else { System.out.println("Nothing!"); } } }
Output
85000
Advertisements