Find max and min of values from list of values using Java stream

Share post:

Java stream get max value from list of values

If we have a list of values eg: 8,18,6,10,-1. how to print the max and minimum values using stream?

Quick Answer:

We can use the max() and min() functions using with the stream to achieve this, Check the code below.

package com.sp.lab;

import java.util.*;

public class FindMaxStream {
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(1,9,3,8);
int a= integerList.stream().mapToInt(s -> s).max().orElseThrow(NoSuchElementException::new);
System.out.println("max:"+a);
int a1= integerList.stream().mapToInt(s -> s).min().orElseThrow(NoSuchElementException::new);
System.out.println("min:"+a1);
}
}

Smashplus

Insights

Share post: