- Passing a lambda expression to a method as a parameter. Examples
- Related topics
- Pass a Function as a Parameter in Java
- Use an Instance of an interface to Pass a Function as a Parameter in Java
- Use java.lang.reflect.Method to Pass a Function as a Parameter in Java
- Related Article - Java Function
- How to pass a function as a parameter in Java?
Passing a lambda expression to a method as a parameter. Examples
A lambda expression can be passed to a method as an argument. The declaration of such a method must contain a reference to the corresponding functional interface. This reference is obtained as a parameter of the method that processes the lambda expression.
There are two ways to pass a lambda expression to a method:
- passing the lambda expression directly. This method works well for lambda expressions with few operators;
- passing a reference to the functional interface that is associated with the lambda expression. In this case, a reference to the interface is pre-declared. The lambda expression code is then assigned to this reference. This technique is appropriate when the lambda expression becomes too long to be embedded in a method call.
2. An example showing how to pass a lambda expression to a method
Suppose you want to pass a lambda expression to the method, which calculates the average of three numbers. To do this, the following sequence of steps is performed.
Way 1. First, the functional interface IAverage is declared, containing a method that receives three parameters and returns a result of type double
// An interface that declares a method for calculating // the average of 3 numbers. interface IAverage < double Avg(double a, double b, double c); >
The next step is to declare a method that will receive a reference to the IAverage interface as a parameter. This method can be placed in some class as shown below
// The class containing the implementation of the method that // receives as a parameter a reference to the IAverage interface class SomeClass < // A method that displays the average of three numbers. // The method receives a reference to the IAverage functional interface. // The program code for calculating average is formed in a lambda expression, // which is passed as an argument to this method. public void PrintAverage(IAverage ref) < System.out.println("ref color: #0000ff;">PrintAverage() method like this:. public static void main(String[] args) < // Client code - demonstrates passing a lambda expression to a method // 1. Create an instance of the SomeClass class SomeClass obj = new SomeClass(); // 2. Call the PrintAverage() method and pass a lambda expression to it obj.PrintAverage((a,b,c) -> (a+b+c)/3.0); > .Way 2. With this method, a reference to the interface is pre-formed. A lambda expression is then assigned to this reference. This reference is then passed to the PrintAverage() method. This approach is useful when the code for the lambda expression is too large and makes the method invocation difficult to read. The following is a modified code from the previous example.
. public static void main(String[] args) < // Client code - demonstrates passing a lambda expression to a method // 1. Declare a reference to the IAverage interface IAverage ref; // 2. Assign a lambda expression that calculates average // of 3 values to the IAverage interface reference ref = (a, b, c) -> (a+b+c)/3.0; // 3. Create the instance of SomeClass class SomeClass obj = new SomeClass(); // 4. Invoke method PrintAverage() obj.PrintAverage(ref); >3. Examples of solving problems in which a lambda expression is passed as an argument to a method
3.1. The maximum value in the array of numbers. Template functional interface. Passing a lambda expression to a static method
Task. Develop a lambda expression that calculates the maximum value in an array of numbers that have some numeric type T . Implement passing a lambda expression to a static method. In the method, print the maximum value to the screen.
Solution. The task declares a generic (template) functional interface IArray . The interface defines one method that receives an array of numbers of type T and returns a value of type T .
The Lambda class is the main class in the program that contains the main() function. In the main() function, the following are formed:
- lambda expression that evaluates the maximum value in array A , which is an input parameter. Note that array A is specified by the parameter of the lambda expression without brackets [ ] ;
- an array of integers that is passed to the static Solution() method.
For demonstration purposes, the Lambda class implements a static templated Solution() method that takes two parameters:
- an array of generic type T, in which to find the maximum element;
- a lambda expression that is passed to the method as a ref reference to the IArray functional interface.
The text of the solution to the problem is as follows
// Functional interface IArray interface IArrayextends Number> < // Method for finding the maximum in an array of numbers T Max(T[] array); > // A class containing methods that implement the lambda expression // and test the program's operation. public class Lambda < // A static template method that takes a lambda expression as a parameter. // The method operates on type T. // The T type is limited to the Number type, which means // that the T type can only be a numeric type (double, float, int, . ). // Method parameters: // - array of numbers of type T; // - a reference to the IArray interface that implements the lambda expression. public static extends Number> void Solution(T[] array, IArray ref) < // Print the maximum value in the array T max = ref.Max(array); System.out.println("max color: #800080;"> public static void main(String[] args) < // Generate a lambda expression that computes the maximum value // in the array of integers IArray refInt = (A) -> < Integer max = A[0]; for (int i=0; ilength; i++) if (max return max; >; // Create an array of integers Integer[] A = < 7, 3, 2, 8, 8, 4 >; // Pass lambda expression to Solution() method Lambda.Solution(A, refInt); // max = 8 > >
The result of the program
3.2. Sorting an array of strings using the insert method. The sorting algorithm is passed by the lambda expression
The example demonstrates the use of a lambda expression to sort an array of strings by inserting.
// A functional interface for sorting an array of strings interface ISortStrings < void Sort(String[] array); > // The class that contains the method to which the lambda expression is passed class SortMethods < // A method that sorts and displays sorted strings. // The lambda expression is passed to the method as a ref. void SortStrings(String[] AS, ISortStrings ref) < // Sort AS array ref.Sort(AS); // Print AS array for (String s : AS) System.out.println(s); > > public class Lambda2 < public static void main(String[] args) < // 1. Declare a reference to ISortStrings ISortStrings ref; // 2. Define a lambda expression for the insertion sort algorithm. // Sorting occurs in ascending order: A. Z ref = (s) -> < String tmp; for (int i=0; ilength-1; i++) for (int j=i; j>=0; j--) if (s[j].compareTo(s[j+1])>0) < tmp = s[j]; s[j] = s[j+1]; s[j+1] = tmp; >>; // 3. Form a testing array of strings String[] AS = < "afd", "jkl", "jprst", "mno", "axf", "aaa", "bcd", "ghi" >; // 4. Call the sort method SortMethods() and pass it to a lambda expression SortMethods obj = new SortMethods(); obj.SortStrings(AS, ref); > >
The result of the program
aaa afd axf bcd ghi jkl jprst mno
3.3. Calculating the number of occurrences of an element in an array. Generalized functional interface
Implement a lambda expression that determines the number of occurrences of element in the array. A lambda expression implements a generic functional interface. Implement a class method that takes a lambda expression as a parameter and outputs the number of occurrences of an element in an array. Demonstrate how the method works in the main() function.
Related topics
Pass a Function as a Parameter in Java
- Use an Instance of an interface to Pass a Function as a Parameter in Java
- Use java.lang.reflect.Method to Pass a Function as a Parameter in Java
This tutorial will discuss how to pass a function as a parameter to another function in Java.
We will discuss two different methods to pass a function as a parameter in Java.
Use an Instance of an interface to Pass a Function as a Parameter in Java
In this method, you need to write the function you need to pass as a parameter in a class implementing an interface containing that method’s skeleton only.
The below example illustrates this.
We define an interface Callable which contains the function skeleton that we plan to pass as a parameter. Next, we define a class that implements Callable and includes the full definition of the function. This function can be passed to another function like newFunction(Callable callable, int param) where callable represents an instance of the interface Callable .
A full working example is shown in the code below.
interface Callable public void call(int param); > class Test implements Callable public void call(int param) System.out.println( param ); > > public class HelloWorld public static void invoke(Callable callable, int param) callable.call(param); > public static void main(String []args) Callable cmd = new Test(); invoke(cmd, 10); > >
Use java.lang.reflect.Method to Pass a Function as a Parameter in Java
We have a function functionToPass which we need to pass as a parameter to the function outerFunction .
There is no difference in how we define functionToPass ; however, we need to follow a specific syntax to define the outerFunction : outerFunction(Object object, Method method, param1, param2, . ) .
Have a look at the example below:
import java.lang.reflect.Method; public class Main public void functionToPass(String message) String [] split = message.split(" "); for (int i=0; isplit.length; i++) System.out.println(split[i]); > public void outerFunction(Object object, Method method, String message) throws Exception Object[] parameters = new Object[1]; parameters[0] = message; method.invoke(object, parameters); > public static void main(String[] args) throws Exception Class[] parameterTypes = new Class[1]; parameterTypes[0] = String.class; Method functionToPass = Main.class.getMethod("functionToPass", parameterTypes[0]); Main main = new Main(); main.outerFunction(main, functionToPass, "This is the input"); > >
Here is another example of the same method. We are creating a function that also has a return value.
import java.lang.reflect.Method; public class Main public int functionToPass(String message) return message.length(); > public void outerFunction(Object object, Method method, String message) throws Exception Object[] parameters = new Object[1]; parameters[0] = message; System.out.println(method.invoke(object, parameters)); > public static void main(String[] args) throws Exception Class[] parameterTypes = new Class[1]; parameterTypes[0] = String.class; Method functionToPass = Main.class.getMethod("functionToPass", parameterTypes[0]); Main main = new Main(); main.outerFunction(main, functionToPass, "This is the input"); > >
Related Article - Java Function
How to pass a function as a parameter in Java?
In Java, you can pass a function as a parameter using a functional interface. A functional interface is an interface that has a single abstract method. You can create a functional interface by annotating an interface with the @FunctionalInterface annotation.
Here's an example of how you can create a functional interface and pass a function as a parameter in Java:
@FunctionalInterface interface Function < void apply(); > public class Main < public static void main(String[] args) < // Create a function that prints "Hello, World!" Function sayHello = () -> System.out.println("Hello, World!"); // Pass the function as a parameter to the runFunction() method runFunction(sayHello); > public static void runFunction(Function func) < func.apply(); >>
In this example, the Function interface is a functional interface that has a single abstract method called apply() . The runFunction() method takes a Function as a parameter and calls the apply() method on it when it is called.
I hope this helps! Let me know if you have any questions.