Java 8 Stream min & max method Example

7 years ago Lalit Bhagtani 0

In this tutorial, we will learn about Java 8 stream min() and max() method.

java 8 stream min

min() Method :-

This method takes one Comparator as an argument and return the minimum element of the stream.

Syntax :-  Optional<T> min(Comparator<? super T> comparator)

Reference :-  min() method JavaDocs

Problem Statement :-

You have given a list of integers, find the minimum integer which is greater than 5 and less than 20.

Java 7 :- To implement this, first iterate through the list of integers. Check if any integers is greater than 5 and less than 20, if it match the given criteria store it in new list. Now sort the new list in ascending order and the first element is the result. 

  public static void main(String[] args){
 
    Integer[] num = new Integer[]{2,4,12,7,16,17,19,20,34,56};
    List<Integer> numList = Arrays.asList(num);
 
    List<Integer> filterList = new ArrayList<Integer>();
    for(Integer number : numList){
      if(number > 5 && number < 20){
        filterList.add(number);
      }
    }
 
    Integer[] filterArr = new Integer[filterList.size()];
    filterList.toArray(filterArr);
    Arrays.sort(filterArr);
    System.out.println(filterArr[0]);
  }

Java 8 :- Implementation using stream API.

  public static void main(String[] args){
 
    Integer[] num = new Integer[]{2,4,7,12,16,17,19,20,34,56};
    List<Integer> numList = Arrays.asList(num);
 
    Optional<Integer> min = numList.stream().filter( e -> e > 5 && e < 20) .min((x,y) -> Integer.compare(x, y)); 
    System.out.println(min.get());
  }

Result :- 

7

max() Method :-

This method takes one Comparator as an argument and return the maximum element of the stream.

Syntax :-  Optional<T> max(Comparator<? super T> comparator)

Reference :-  max() method JavaDocs

Problem Statement :-

You have given a list of integers, find the maximum integer which is greater than 10 and less than 20.

Java 7 :- To implement this, first iterate through the list of integers. Check if any integers is greater than 10 and less than 20, if it match the given criteria store it in new list. Now sort the new list in ascending order and the last element is the result. 

 public static void main(String[] args){
 
   Integer[] num = new Integer[]{2,4,12,7,16,17,19,20,34,56};
   List<Integer> numList = Arrays.asList(num);
 
   List<Integer> filterList = new ArrayList<Integer>();
   for(Integer number : numList){
     if(number > 10 && number < 20){
       filterList.add(number);
     }
   }
 
   Integer[] filterArr = new Integer[filterList.size()];
   filterList.toArray(filterArr);
   Arrays.sort(filterArr);
   System.out.println(filterArr[filterArr.length-1]);
 }

Java 8 :- Implementation using stream API.

  public static void main(String[] args){
 
    Integer[] num = new Integer[]{2,4,7,12,16,17,19,20,34,56};
    List<Integer> numList = Arrays.asList(num);
 
    Optional<Integer> min = numList.stream().filter( e -> e > 10 && e < 20) .max((x,y) -> Integer.compare(x, y)); 
    System.out.println(min.get());
  }

Result :- 

19

That’s all for Java 8 Stream min & max method Example. If you liked it, please share your thoughts in comments section and share it with others too.