Java 17 Recipes
-8. Passing Lambda Expressions to Methods
Download 3.2 Mb. Pdf ko'rish
|
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 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 [] args){ Double returnVal; List 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
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
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
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
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 |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling