September 2018
Intermediate to advanced
802 pages
19h 30m
English
Using lambda expressions, we can rewrite the preceding code as follows:
Vehicle vehicle = new VehicleImpl(3000, 200);SpeedModel speedModel = (t, wp, hp) -> { double v = 2.0 * hp * 746 * t * 32.17 / wp; return (double) Math.round(Math.sqrt(v) * 0.68);};vehicle.setSpeedModel(speedModel);System.out.println(vehicle.getSpeedMph(10.)); //prints: 122.0
We will discuss the lambda expressions format in the next recipe. For now, we would like only to point out the importance of functional interfaces for an implementation such as the preceding one. As you can see, there is only the name of the interface specified and no method name at all. That is possible because a functional interface has only one method that has to be implemented, ...
Read now
Unlock full access