Java Stream get min value

Description

Java Stream get min value



import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {

  public static void main(String[] args) {

    // Create a list of Integer values.
    ArrayList<Integer> myList = new ArrayList<>();
    myList.add(7);//from   w w w  .  jav a  2 s  . com
    myList.add(8);
    myList.add(1);
    myList.add(2);
    myList.add(7);
    myList.add(5);

    System.out.println("Original list: " + myList);

    // Obtain a Stream to the array list.
    Stream<Integer> myStream = myList.stream();

    // Obtain the minimum and maximum value by uses of min(),
    // max(), isPresent(), and get().
    Optional<Integer> minVal = myStream.min(Integer::compare);
    if (minVal.isPresent())
      System.out.println("Minimum value: " + minVal.get());
  }
}



PreviousNext

Related