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:
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!