I have the following code:
@Test
public void testAverageFromArray() {
final Double[] dbls = { 1.1, 1.2, 1.3, 1.4, 1.5 };
final double av = Stream.of(dbls).mapToDouble(d -> d).average().getAsDouble();
assertEquals(1.3, av, 0);
}
Question: Is it possible to replace the d -> d lambda with some other syntax? It seems needless.
*I wasnt sure about the title of this question - please edit if its off the mark.
thanks
The lambda d -> d
is not needless. What happens is that you need to provide a function T -> double
. In fact d -> d
is a function Double -> double
because the unboxing is done automatically (since Java 5).
You could always replace it with a method reference .mapToDouble(Double::doubleValue)
to make it clear that it unboxes the double value for the Double instance you're processing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With