Open In App

Java | Collectors minBy(Comparator comparator) with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Collectors minBy(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 minimal element according to a given Comparator, described as an Optional<T>.

Syntax:

public static 
    <T> Collector<T, ?, Optional<T>> 
        minBy(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 minimal value in accordance with the comparator passed.

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

Program 1: To demonstrate minBy() using naturalOrder()




// Java code to show the implementation of
// Collectors minBy(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 minBy(Comparator comparator)
        // and finding the minimum element
        // in reverse order
        Optional<String> obj = s
                                   .collect(Collectors
                                                .minBy(Comparator
                                                           .naturalOrder()));
  
        // 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

Program 2: To demonstrate minBy() using reverseOrder()




// Java code to show the implementation of
// Collectors minBy(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 minBy(Comparator comparator)
        // and finding the minimum element
        // in reverse order
        Optional<String> obj = s
                                   .collect(Collectors
                                                .minBy(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:

5

Program 3: To demonstrate minBy() when no values are passed.




// Java code to show the implementation of
// Collectors minBy(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 minBy(Comparator comparator)
        // and finding the minimum element
        // in reverse order
        Optional<String> obj = s
                                   .collect(Collectors
                                                .minBy(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


Last Updated : 06 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads