Lambda expressions in Java

How to use lambda expressions in Java 8?

One of the biggest features introduced in Java 8 is the lambda expressions. Lambda expression facilitates functional programming. Lambda expression is similar to a method (implementation). It can have arguments, a method body and return type. It can also be called as an anonymous method i.e. a method without a name (likewise an anonymous class which is a class without a name). Lambda expressions are said to be of type functional interface.

Functional Interfaces:

An interface with only one abstract method is called a functional interface. Event listeners in Java are often defined as Java interfaces with a single method. For example,

This Java interface defines a single method which is called whenever an action is performed on an element (to which the listener is attached).

In versions prior to Java 8, to add an event listener, you would define an anonymous inner class implementation as below,

addBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
              System.out.println(“Add button Clicked!”);

This syntax looks unclear. In this case we can use lambda expression to make the syntax more clear.

Syntax of lambda expressions:

Lambda expression syntax

() -> System.out.println(“Lambda expression without parameter”);

(param) -> System.out.println(“Lambda expression with single parameter: “+ param);

// When a lambda expression takes a single parameter, you can also omit the parentheses.

(param1, param2) -> System.out.println(“Lambda expression with multiple parameters: ” + param1 + “, ” + param2);


If your lambda expression needs to contain multiple lines, you can enclose the lambda function body inside the curly brace { } as below,
(param1, param2) -> {
      System.out.println(“Parameter 1 ” + param1);
      System.out.println(“Parameter 2 ” + param2);

The action listener implementation can now be written as,
addBtn.addActionListener( e -> System.out.println(“Add button Clicked!”););

The parameter(s) in the lambda expression should match the number and type of arguments in the original functional interface. From the above statement we can see that lambda expression can be passed as an argument to another method.

Returning value from lambda expression:

You can a return value from a lambda expression as below (just as a function can),
(param) -> {
         System.out.println(“param: ” + param);
         return “return value”;

A lambda expression can be assigned to a variable as explained in the below example,

Using lambda expression for iterating collections:
Prior to java 8, you could iterate a collection as below,

List<Integer> numbers = Arrays.asList (1, 2, 3, 4, 5, 6);
for (int i = 0; i < numbers.size(); i++) {
System.out.println (i);


for (int i : numbers) {
System.out.println (i);


Iterator<Integer> itr = numbers.iterator();
While(itr. hasNext()) {

In java 8, using lambdas, you can simply write like this,
numbers.forEach (i ->System.out.println (i));

Key notes of lambda expressions:

• Lambda expressions can be used in case of functional interface definition with a single method (e.g. event listener implementation).
• Lambda expression can be used as a parameter to another method
• Lambda expression can be used for iteration collections.
• Lambda expression can have zero to any number of parameters.
• If there are no parameters to be passed, then an empty parenthesis is given.
• If there is only one parameter, parenthesis can be omitted
• Type of the passed parameter can be explicitly declared or can be taken from context.
• Lambda expression body can have any number of statements. If the body has more than one statement, then it has to be enclosed within curly braces.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

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