Java Reference
In-Depth Information
System.out.println("METHODA Value: " + METHODA);
System.out.println(passedIn);
};
l1.accept(CLASSA);
l1.accept(passedIn);
}
Note that the string that is passed into lambdaInMethod() is assigned a new
value just before the lambda expression is invoked. Therefore, the passedIn variable
is no longer effectively final, and lambda expressions cannot introduce a new level of
scope. Consequently, the lambda expression does not have access to the passedIn
variable from within the context of the expression.
6-8.
Passing
Lambda
Expressions
to
Methods
Problem
A lambda expression has been created to encapsulate some functionality. You would
like to take that functionality and pass it into a method as an argument, so that the
method implementation can take advantage of the expression.
Solution
Create portable functions using lambda expressions by implementing a functional in-
terface and then assigning the lambda expression to a variable of the same type as the
interface. The variable can be passed to other objects as an argument.
The following class, PassingLambdaFunctions , contains a calculate()
method, which will be used to perform calculations of any type given an array of val-
ues. Note that the calculate() method accepts a Func-
tion<List<Double>,Double> and an array of Double values as arguments.
Search WWH ::




Custom Search