Java examples for Lambda Stream:DoubleFunction
Compute the maximum (double) value of applying the given function to the given values using Lambda.
/*/*w ww. j a v a 2 s .c o m*/ * www.javagl.de - Viewer - Functions * * Copyright (c) 2013-2015 Marco Hutter - http://www.javagl.de * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ //package com.java2s; import java.util.function.DoubleFunction; public class Main { /** * Compute the maximum (double) value of applying the given function to * the given values.<br> * <br> * If the function returns <code>null</code> or NaN for any value, this * will be ignored. If the function returns <code>null</code> for all * values, then <code>null</code> will be returned. If the function * returns NaN for all values, then NaN will be returned. * * @param function The function * @param values The values * @return The maximum */ private static Number computeMax( DoubleFunction<? extends Number> function, Iterable<Double> values) { Number max = null; for (double x : values) { Number y = function.apply(x); max = max(max, y); } return max; } /** * Returns the maximum (double) of the given values.<br> * <br> * If either value is <code>null</code>, then the other value will be * returned. If both values are <code>null</code>, then <code>null</code> * will be returned. If neither value is <code>null</code>, and either * value is NaN, then the other value will be returned. If both values * are NaN, then NaN will be returned. * * @param n0 The first value * @param n1 The second value * @return The maximum */ static Number max(Number n0, Number n1) { if (n0 == null) { return n1; } if (n1 == null) { return n0; } double d0 = n0.doubleValue(); double d1 = n1.doubleValue(); if (Double.isNaN(d0)) { return d1; } if (Double.isNaN(d1)) { return d0; } return Math.max(d0, d1); } }