import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
*
* Program:
* Find Max Value of List using
* Java 8 Stream API
*
* Example by Code2care.org
*
*/
public class Example {
public static void main(String[] args) {
List<Integer> numList = new ArrayList<>();
numList.add(10);
numList.add(15);
numList.add(5);
numList.add(20);
numList.add(45);
numList.add(30);
Optional<Integer> maxValue = numList.stream().max(Integer::compareTo);
maxValue.ifPresent(maxVal -> System.out.println("Max value in "+ numList + " is: " + maxVal));
}
}
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!