Open In App

Java | Collectors maxBy(Comparator comparator) with Examples

Collectors maxBy(Comparator<? super T> comparator) is used to find an element according to the comparator passed as the parameter. It returns a Collector that produces the maximal element according to a given Comparator, described as an Optional<T>.

Syntax:

public static 
    <T> Collector<T, ?, Optional<T>> 
        maxBy(Comparator<? super T> comparator)

where the terms used are as follows:

Parameters: This method takes a parameter comparator of type Comparator, which is a comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don’t have a natural ordering.

Return Value: This method returns a Collector that produces the maximal value in accordance with the comparator passed.

Below are some examples to illustrate the implementation of maxBy():

Program 1:




// Java code to show the implementation of
// Collectors maxBy(Comparator comparator) function
  
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
class GFG {
    // Driver code
    public static void main(String[] args)
    {
        // creating a Stream of strings
        Stream<String> s = Stream.of("2", "3", "4", "5");
  
        // using Collectors maxBy(Comparator comparator)
        // and finding the maximum element
        // in reverse order
        Optional<String> obj = s
                                   .collect(Collectors
                                                .maxBy(Comparator
                                                           .reverseOrder()));
  
        // if present, print the element
        // else print the message
        if (obj.isPresent()) {
            System.out.println(obj.get());
        }
        else {
            System.out.println("no value");
        }
    }
}

Output:
2

Example 2:




// Java code to show the implementation of
// Collectors maxBy(Comparator comparator) function
  
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // creating a Stream of strings
        Stream<String> s = Stream.of();
  
        // using Collectors maxBy(Comparator comparator)
        // and finding the maximum element
        // in reverse order
        Optional<String> obj = s
                                   .collect(Collectors
                                                .maxBy(Comparator
                                                           .reverseOrder()));
  
        // if present, print the element
        // else print the message
        if (obj.isPresent()) {
            System.out.println(obj.get());
        }
        else {
            System.out.println("no value");
        }
    }
}

Output:
no value

Article Tags :