Method References in Lambda Expressions(part-2)
For the better understanding about the method references in lambda expressions , go through
Method References In Lambda Expressions :
Method references in Lambda expressions are implemented when
You simply receiving an parameter — and pass through it with out modifying that parameter
Let’s Observe the above code Here e is the parameter we received and we simply passed through the method println() without modifying it . so we implemented as
object of the class(Instance or static)::methodname
Method references can be implemented as 3 ways
(1) They can be implemented to the parameters passing to the instant Methods
(2) They can be implemented to the parameters passing to the static Methods
(3) They can be implemented to the parameters which is used as a target
parameters passing to the instant Methods:
In the above code println() is the instance Method , System.out is the instance object and it is implemented to the parameter e
parameters passing to the static Methods
In the above example String.valueOf(e), String is a class and valueOf(e) is a Static Method and e is the parameter,we are passing through it.
parameters which is used as a target
In the above .map(e -> e.toString()) , e is used as a target for the toString() method and the method is a static method of String class
Method References for the Multiple Parameters
Limitations :
- If you manipulate the parameters , you cannot use it
- You cannot use them , if there is a conflict between the instant method name and static method name
- if the order is not same, you cannot use the method references for the methods which contains the multiple parameters
Let’s Observe below example How Lambda expressions plays an important key role while converting imperative style of programming to the functional style of programming with the help of streams
problem statement : for the given the values, double the even numbers and give total