Open In App

Java | Collectors minBy(Comparator comparator) with Examples

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:

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

Article Tags :