Description
Java just started to support lambdas in its language, with this new feature, they brought into play some syntax sugar coating to make code more concise. I thought it look pretty neat and wanted to know if that could be an enhancement of the language.
So, for those who might not know what method reference actually is, I'm going to give a definition for this and then show you an example of how it is used.
Method reference : shortcuts that you can use anywhere you would use a lambda.
Oracle defines 4 types of method references
Reference to a static method (ContainingClass::staticMethodName)
Reference to an instance method of a particular object (ContainingObject::instanceMethodName)
Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName
Reference to a constructor (ClassName::new)
As an example,
Here's the first version in Java (Lambda)
list.sort(comparing( (Apple a) -> a.getWeight() ));
Now here's the version using method reference
list.sort(comparing(Apple::getWeight));
It just makes code a bit shorter when using lambda. I'm well aware that Typescript has delegates and that it could be encapsulated (lambdas) within a delegate. I stumble upon that Java 8 feature and thought that a similar feature would be quite nice.
I'd like to start a discussion on this topic.