Java 17 Recipes


-8. Passing Lambda Expressions to Methods


Download 3.2 Mb.
Pdf ko'rish
bet125/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   121   122   123   124   125   126   127   128   ...   245
Bog'liq
Java 17 Recipes

6-8. Passing Lambda Expressions to Methods
 Problem
A lambda expression has been created to encapsulate some functionality. You want 
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 
interface and 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 performs calculations of any type given an array of values. Note that the 
calculate() method accepts a Function,Double> and an array of 
Double values as arguments.
public class PassingLambdaFunctions {
/**
* Calculates a value based upon the calculation function that 
is passed
* in.
* @param f1 Double input value
* @param args Double input value
* @return
*/
public Double calculate(Function, Double> f1,
Double [] args){
Double returnVal;
List varList = new ArrayList();
int idx = 0;
while (idx < args.length){
varList.add(args[idx]);
idx++;
}
ChapTer 6 LaMbda expressIons


231
returnVal=f1.apply(varList);
return returnVal;
}
}
To use the calculate method, a lambda expression that implements 
Function,Double> must be passed as the first argument to the 
calculate() method, along with an array of Double arguments that contains the value 
to be used within the calculation. In the following class, a function for calculating 
volume is generated using a lambda expression. It is assigned to a variable identified 
as volumeCalc of type Function,Double>. Another lambda expression 
creates a function for calculating area, and it is assigned to a variable of the same 
type, identified as areaCalc. In separate calls, these variables are then passed to the 
PassingLambdaFunctions.calculate() method, along with an array of values, resulting 
in the calculated answer.
public class MainClass {
public static void main(String[] args){
double x = 16.0;
double y = 30.0;
double z = 4.0;
// Create volume calculation function using a lambda.
The calculator checks to ensure that the array contains the 
three necessary elements for the calculation.
Function, Double> volumeCalc = list -> {
if(list.size() == 3){
return list.get(0) * list.get(1) * list.get(2);
} else {
return Double.valueOf("-1");
}
};
Double[] argList = new Double[3];
argList[0] = x;
argList[1] = y;
argList[2] = z;
ChapTer 6 LaMbda expressIons


232
// Create area calculation function using a lambda.
This particular calculator checks to ensure that the array only 
contains two elements.
Function, Double> areaCalc = list -> {
if(list.size() == 2){
return list.get(0) * list.get(1);
} else {
return Double.valueOf("-1");
}
};
Double[] argList2 = new Double[2];
argList2[0] = x;
argList2[1] = y;
PassingLambdaFunctions p1 = new PassingLambdaFunctions();
// Pass the lambda expressions to the calculate() method, along 
with the argument lists.
System.out.println("The volume is: " + p1.calculate(volumeCalc, 
argList));
System.out.println("The area is: " + p1.calculate(areaCalc, 
argList2));
}
}
The following is the result.
The volume is: 1920.0
The area is: 480.0
ChapTer 6 LaMbda expressIons


233

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   121   122   123   124   125   126   127   128   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling