Java 8 identity mapping lambda?

java, java-8, lambda

Solution

No need:

System.out.println("Average area: "
     + Arrays.asList(rects)
       .parallelStream()
       .mapToDouble((RectangularShape r) -> (r.getWidth() * r.getHeight()))
       .average();

(Also, you may find `Stream.of(rects).parallel())` preferable to `Arrays.asList(rects).parallelStream()`.)

Problem

I want to find the average area of a few rectangles using aggregate operations in Java 8. ``` Rectangle[] rects = new Rectangle[]{ new Rectangle(5, 10, 20, 30), new Rectangle(10, 20, 30, 40), new Rectangle(20, 30, 5, 15) }; System.out.println("Average area: " + Arrays.asList(rects) .parallelStream() .map((RectangularShape r) -> (r.getWidth() * r.getHeight())) .collect(Collectors.averagingDouble(o -> o))); // I don't like this "o -> o" System.out.println("Expected: 625"); ``` However, I find the `o -> o` required by `averagingDouble` kind of silly. Is there a more intuitive replacement for this lambda (maybe even a stock identity lambda somewhere)?

Original source