Example usage for java.util.function BinaryOperator maxBy

List of usage examples for java.util.function BinaryOperator maxBy

Introduction

In this page you can find the example usage for java.util.function BinaryOperator maxBy.

Prototype

public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) 

Source Link

Document

Returns a BinaryOperator which returns the greater of two elements according to the specified Comparator .

Usage

From source file:Main.java

public static void main(String[] args) {
    BinaryOperator<Integer> bi = BinaryOperator.maxBy(Comparator.reverseOrder());
    System.out.println(bi.apply(2, 3));
}

From source file:Main.java

public static void main(String... args) {
    Comparator<Food> dishCaloriesComparator = Comparator.comparingInt(Food::getCalories);
    BinaryOperator<Food> moreCaloricOf = BinaryOperator.maxBy(dishCaloriesComparator);
    Food o = Food.menu.stream().collect(Collectors.reducing(moreCaloricOf)).get();
    System.out.println(o);//ww w . ja v  a2  s.  c o m

}

From source file:org.apache.storm.daemon.logviewer.utils.LogCleaner.java

/**
 * Return the last modified time for all log files in a worker's log dir.
 * Using stream rather than File.listFiles is to avoid large mem usage
 * when a directory has too many files./*from  w ww. j a v a 2 s  . c om*/
 */
private long lastModifiedTimeWorkerLogdir(File logDir) {
    long dirModified = logDir.lastModified();

    DirectoryStream<Path> dirStream = null;
    try {
        dirStream = directoryCleaner.getStreamForDirectory(logDir);
    } catch (IOException e) {
        return dirModified;
    }

    if (dirStream == null) {
        return dirModified;
    }

    try {
        return StreamSupport.stream(dirStream.spliterator(), false).reduce(dirModified, (maximum, path) -> {
            long curr = path.toFile().lastModified();
            return curr > maximum ? curr : maximum;
        }, BinaryOperator.maxBy(Long::compareTo));
    } catch (Exception ex) {
        LOG.error(ex.getMessage(), ex);
        return dirModified;
    } finally {
        if (DirectoryStream.class.isInstance(dirStream)) {
            IOUtils.closeQuietly(dirStream);
        }
    }
}