Effective Java! Prefer Method References to Lambdas
One of the main benefits of lambdas is the conciseness of the code versus the use of an anonymous class. Even so, there are times that even lambdas end up with unnecessary boilerplate. In these cases we can often make use of method references which is something we even saw used in our last post.
Method references are really just that, a reference to a method that allows us to skip the boilerplate of simply passing parameters from one place to another. Let’s look at an extremely simple example:
IntStream.of(1,2,3).reduce((a, b) -> Integer.sum(a, b));
This is pretty simple code that simply creates an IntStream
, sums all the values together, and returns the sum. We use a lambda as part of the reduce
call. This isn't very verbose but the (a, b) ->
provides us no value. Looking at this same code but with method references.
IntStream.of(1,2,3).reduce(Integer::sum);
This indeed is simpler and I do like that it focuses on what is being done not how to do it. Not showing how the sum is accomplished by calling out directly that it is a sum that is happening.
Almost in all cases we should use a method reference instead of the equivalent lambda. The reason for this is most of the time it increases the readability. You can even use this capability to your advantage in that you can write a method to contain all of your business logic and give it a descriptive name and call it via a method reference. If this is…