Java Lambda Expressions

What are Lambda Expression?

Lambda expressions were introduced in Java 1.8 and it’s the first step towards functional programming.Lambda expressions allow you to treat functionality as a method arguments.The functionality can be passed around to different methods as lambda expressions and executed on demand.It also make the code clean and concise.

In short lambda expressions are basically blocks of code that can be passed around methods as arguments ,assigned to variables and returned from function calls.

Why they are used?

Anonymous classes enable implement a base class or an interface without giving a name.But for classes/interfaces where there is only one method even anonymous classes looks verbose and lengthy.Lambda expression lets you implement single method classes/interfaces in a much more compact way.

Lambda Expression and Functional Interfaces

Lambda expression and functional interfaces go hand in hand. Lambda expressions are used to implement the abstract method of the functional interface. Here’s some of the most commonly used functional interfaces.You can find the complete list of functional interfaces at https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

Consumers

It’s a built in functional interface in the java.util.function package.It can be used in all the context where an object needs to be consumed and it has a void return type.For instance it can accept some object ,perform some operation on the object and display the result without returning any result.It accepts a single input argument.

Predicates

A functional interface and represents an operation that takes a single input and returns a boolean value.

Function

It represents a function that accepts one argument and returns a result.For instance it can accept an object ,perform some operation on the object and return the modified object.

Suppliers

It represents a function that do not take any arguments but returns a result.

In the lambda expression any of these and many other functional interfaces can be implemented depending on the requirements.

Lambda Expression Syntax

(argument-list) -> {body}  

  • Argument List can be empty or we can have as many arguments as we want
  • Arrow Token (->):- It is used to link arguments with the body
  • Body :- It contains expressions and statements .The curly braces are optional if there is only one statement

Example

We have an employee class and an interface.

Without Lambda Expression the code will look like this.We are using anonymous class in this case:-

With lambda expressions:-

Lambda expressions is pretty versatile feature in Java 1.8 and this article gives you a glimpse of it.

Let me know you inputs on lambda expressions.I would be glad to hear from my readers. Happy Coding 🙂

Published by Anna

Just a simple girl who loves to code. .I am primarily a Java developer by profession and I am here to share my love for coding with you all I hope you enjoy reading my blog. Happy Coding Guys :-)

Leave a comment

Your email address will not be published. Required fields are marked *