lambda expressions & functional interfaces in java 8 explained.

aditya chaudhari
JavaDeveloperDiary — JDD
2 min readSep 30, 2021

Q. What is lambda expressions ?
In functional programming paradigm, lambdas are functions without names.
Lambda expressions follows “data as a code” philosophy where data itself is code to some other data. eg. functions can expect argument as another function.

In java context, Lambda expressions are similar like anonymous inner classes but it does not have name , and method definitions like parameters and return type.

Lambda expressions can be only used where functional interface is expected. Lambda expressions let us provide implementation of functional interface directly inline and treat whole expression as instance of a functional interface.

In short lambda expressions are our gateways to use richness of functional programming in java.

Q. What is functional interface in java ?
Functional interfaces are interfaces which has only one abstract methods. marking it with @FunctionalInterface is not mandatory but for custom functional interfaces if we add this this will help throw relevant logging and errors. Below is the list of already available functional interfaces.

A functional interface may also have zero or more static methods and default methods, which are implemented right within the interface.

Code Examples of already available Functional interfaces :

Repo link to refer and fork about code : https://github.com/adityachaudhari/java8/tree/master/Java8_understanding/src/com/java8/functionalinterfaces

The Java API defines a number of very generic functional interfaces in the java.util.function package.

Q. What is method references in java 8 lambda expressions ?

Method references let us reuse existing method definitions and pass them just like lambdas. In some cases they appear more readable and feel more natural than using lambda expressions.

The :: operator separates the method name from the name of an object or class. There are three principal cases:
object::instanceMethod
Class::staticMethod
Class::instanceMethod

For example, The lambda expression (String s) -> s.toUpperCase() can be rewritten as String::toUpperCase.
please refer code below :

Thanks for reading.
If you have any suggestions regarding topic or post please let me know in comments . If you like content please encourage me to write more by applause on this post.

All java 8 related articles on this publication are here.

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT