- 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
IntStream findAny() method in Java
The findAny() method of the IntStream class in Java is used to return an OptionalInt describing some element of the stream, or an empty OptionalInt if the stream is empty.
The syntax is as follows −
OptionalInt findAny()
Here, OptionalInt is a container object which may or may not contain an int value.
Create an IntStream and add some elements −
IntStream intStream = IntStream.of(20, 35, 50, 60, 80, 100);
Now, return any of the element of the stream using findAny() in Java −
OptionalInt res = intStream.findAny();
The following is an example to implement IntStream findAny() method in Java −
Example
import java.util.*; import java.util.stream.IntStream; public class Demo { public static void main(String[] args) { IntStream intStream = IntStream.of(20, 35, 50, 60, 80, 100); OptionalInt res = intStream.findAny(); if (res.isPresent()) { System.out.println(res.getAsInt()); } else { System.out.println("Nothing!"); } } }
Output
20
Advertisements
To Continue Learning Please Login
Login with Google