Java Stream How to - Output if value present








Question

We would like to know how to output if value present.

Answer

//from  w w w  . j a  va2  s  . c o m
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    Arrays.asList(1,20,40,4)
    .stream()
    .max(Integer::compareTo)
    .ifPresent(max -> System.out.println("max number: " + max));
  }
}

The code above generates the following result.