Java | Collectors maxBy(Comparator comparator) with Examples - GeeksforGeeks

06 Dec, 2018
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:
Interface Collector<T, A, R>: A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel.T: The type of input elements to the reduction operation.A: The mutable accumulation type of the reduction operation.R: The result type of the reduction operation.
T: The type of input elements to the reduction operation.
A: The mutable accumulation type of the reduction operation.
R: The result type of the reduction operation.
Optional: A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.
Comparator: 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.
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");        }    }}
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");        }    }}
no value

Java - util package
Java-Collectors
Java-Functions
java-stream
Java-Stream-Collectors
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Singleton Class in Java