We can make use of the Steam API to find the maximum value from a List in Java 8.
Example:
package org.example;
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
public class MaxListValueJava8Example {
public static void main(String[] args) {
List<Integer> numbersList = Arrays.asList(2, 10, 8, 12, 9, 5);
OptionalInt maxNumber = numbersList.stream()
.mapToInt(Integer::intValue)
.max();
if (maxNumber.isPresent()) {
System.out.println("Maximum number is: " + maxNumber.getAsInt());
} else {
System.out.println("The provided List is empty!");
}
}
}
Output:
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- Fix SharePoint Error - The Managed Metadata Service or Connection is currently not available. The Application Pool or Managed Metadata Web Service may not have been started - SharePoint
- Outlook and Hotmail not working - email issue, message not delivered, send receive problem - Microsoft
- Set width and height for the label in tkinter - Python
- How to install zsh shell on Ubuntu - Ubuntu
- AlertDialog with image using ImageView Example - Android
- Bash - How to check if a Command Failed? - Bash
- Android Studio: Cannot perform refactoring operation - Android-Studio
- MainActivity error: cannot find symbol FloatingActionButton - Android