Java8: Lambdas & Functional Interfaces

Functional interfaces are type of interface which will have one method in it. Lambda expression facilitates functional programming, and simplifies the development. Functional interfaces can be used with lambdas as following.

Photo by Travel Sourced on Unsplash

If you have a close look at following example you will understand how a functional interface is used.

This fn object gives direct access to foo implementation.

Example 2

If you import java.util.function you will see many functional interfaces provided by java itself. Lets see how to use one of them.

In this example we will use predicate functional interface, will return boolean value based on provided implementation. In this example provided implementation is n->n%2==0, which will return true if the value is even.

Example 3

Example 4

In this example we are using consumer interface to pass the second command. compare with example 2.


Read my other blogs:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nuwan Zen

Sometimes A software Engineer, sometimes a support engineer, sometimes a devops engineer, sometimes a cloud engineer :D That’s how the this life goes!