

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
IntStream max() method in Java
The IntStream max() method in the Java IntStream class is used to get the maximum element from the stream. It returns an OptionalInt describing the maximum element of this stream, or an empty optional if this stream is empty.
The syntax is as follows
OptionalInt max()
Here, OptionalInt is a container object which may or may not contain an int value.
Create an IntStream and add some elements in the stream
IntStream intStream = IntStream.of(89, 45, 67, 12, 78, 99, 100);
Now, get the maximum element from the stream
OptionalInt res = intStream.max();
The following is an example to implement IntStream max() method in Java. The isPresent() method of the OptionalInt class returns true if the value is present
Example
import java.util.*; import java.util.stream.IntStream; public class Demo { public static void main(String[] args) { IntStream intStream = IntStream.of(89, 45, 67, 12, 78, 99, 100); OptionalInt res = intStream.max(); System.out.println("The maximum element of this stream:"); if (res.isPresent()) { System.out.println(res.getAsInt()); } else { System.out.println("Nothing!"); } } }
Output
The maximum element of this stream: 100
- Related Questions & Answers
- IntStream asLongStream() method in Java
- IntStream peek() method in Java
- IntStream forEachOrdered() method in Java
- IntStream asDoubleStream() method in Java
- IntStream rangeClosed() method in Java
- IntStream sorted() method in Java
- IntStream findFirst() method in Java
- IntStream map() method in Java
- IntStream flatMap() method in Java
- IntStream iterator() method in Java
- IntStream limit() method in Java
- IntStream parallel() method in Java
- IntStream builder() method in Java
- IntStream empty() method in Java
- IntStream distinct() method in Java
Advertisements