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.
- 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