Overview
In this article, we will understand Stream.max() method added in java 8 with its use and examples in detail.

Stream.max()
Stream.max() is a new addition to the java 8 API which allows you to find the maximum element in a stream.
Maximum element is determined according to the comparator that is supplied as argument to this function.
There are many ways that Stream.max() can used, as
1. Finding the largest element in a stream of numbers,
2. Finding the longest string in a stream of strings,
3. Find an object with maximum value of a field such as an employee with highest salary.

Java docs for Stream.max() state,

Returns the maximum element of this stream according to the provided Comparator

Syntax
Stream.max() is invoked on a stream object and accepts a comparator as argument.
Max element is determined by applying this comparator on stream elements.
max() returns an Optional object containing the max value.
Its syntax is

Optional<T> max(Comparator<? super T> comparator);

If no Comparator is passed, then the maximum element is determined by the natural ordering of the elements.

Stream.max() example
To use Stream.max(), first you need to create a stream.
This can be done in many ways
1. Using the Stream.of() method,
2. Using the Collection.stream() method,
3. Calling stream() method on a list.

Once you have a stream, you can then call the Stream.max() method and pass in a Comparator.
The Comparator will be used to determine the maximum element in the stream.
An example to find the maximum integer in a stream of integers is given below

Stream<Integer> numbers = Stream.of(34,56,1,56,3,34); 
Optional<Integer> maxNumberOp = numbers.max(
                Comparator.comparing(Integer::valueOf));
int max = maxNumberOp.get(); 
System.out.println("Maximum number is: " + max);

Output is

Maximum number is 56

Note that to get the actual max value from an Optional, call its get() method.

There are a number of ways you can create a comparator to supply to max() as shown below

// as a lambda expression
Comparator<Integer> c = (i, j)-> i.compareTo(j);

// with natural order method
Comparator.naturalOrder()

// as an anonymous class
new Comparator<Integer>() {
  public int compare(Integer o1, Integer o2) {
    return o1.compareTo(o2);
  }
}

where the first method is the most concise as it uses another java 8 feature, Lambda expressions.

Stream.max() to find longest word
Below is an example to find the longest word in a stream of string values.

Stream<String> words = Stream.of("the", "quick", "brown", 
                         "fox", "jumps", "over", "the", 
                         "lazy", "dog");
String longestWord = words.max(
                      Comparator.comparing(String::length)).
                     get();
System.out.println("The longest word is: " +  
                     longestWord);

As before, you can use comparator in any of the multiple ways.
Find max in stream of objects
Below is an example of Stream.max() to find the oldest person in a stream of Person objects.

Stream<Person> persons = Stream.of(
                          new Person("A", 50), 
                          new Person("B", 60), 
                          new Person("C", 40)); 
Person oldestPerson = people.max(
                          Comparator.comparing(Person::getAge)
                         ).get(); 
System.out.println("Oldest person is: " + oldestPerson);

Here, Person is a simple java class with two fields: name and age and an overridden toString() method.

Output is

Oldest person is: [Name: B, Age: 60]

Hope the article was useful.