Using Lambda Expressions With Functional Interfaces in Java

Functional Interfaces in Java are a very useful tool that many newer programmers don't utilize. They allow developers to abstract their code so that it can be applied to many different problems. This is especially useful with Lambda expressions which allow functions to be created within a method's parameters. These instructions show how to use a very basic Functional interface called Function. Function has an abstract method called apply that takes one parameter of generic type and returns a generic type. Apply does not have to be defined until the call of the method that calls apply. This is very powerful because it allows programmers to use the same piece of code multiple times only having to change the call to that method.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Create a Java Project

Open an IDE and create a java project, the name is not important. I have named mine "Instructions."

Step 2: Create a Package

Create a new package in the source file, named “instructions.”

Step 3: Create the Converter Class

In the instructions package, create a new class called Converter and Import java.util.function.Function.

Step 4: Create the FunctionTest Class

In the instructions package, create a new class called FunctionTest.

Step 5: Create the Convert Method

In the Converter class, Create a method called "convert" that returns a String s and takes in an int x and a Function f as parameters.

Step 6: Add Type Parameters

Add type parameters Integer and String to the Function f parameter.
This should look like: Function f

Step 7: Calling Apply

Return the result of calling the apply function on f with x and a parameter by return f.apply(x)

Step 8: Main Method

Create a main method in FunctionTest.

Step 9: Start to Call Convert

In the main method of the FunctionTest class start to call the convert method Converter.convert(

Step 10: Choose an Integer

Inside the parentheses, enter an int that you would like to convert to a string. This should look like the picture above.

Step 11: Seperate the Parameters

The next parameter is the Lambda function. With the cursor at the position in the image above, type a comma then a space to delineate between the two parameters.

Step 12: Lambda Function Parameter

Next, you will type the parameters for the lambda function. (Integer x) is our only parameter

Step 13: Lambda Function Body

Following the parameter, type -> to signal that the next text is the body of the function. Type x.toString, close the parentheses, and finish with a semicolon.

Step 14: Assign Result

To make sure the program is working, assign the call to convert to a String variable called result

Step 15: Test

Check that result is equal to the string version of the Integer parameter that you chose. One simple way to do this is with an if statement, shown below.

Be the First to Share

    Recommendations

    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    Discussions