Java 8: Lambda Expression

Java 8: Lambda Expression

·

3 min read

Play this article

This article will explain the following:

  • What a lambda expression is.

  • External VS Internal iteration.

  • Lambda expression within a functional interface.

  • Benefits of using a lambda expression.

What is a lambda expression?

Java 8 has introduced many new features, and lambda expression is one of them.

A lambda expression is a function that doesn’t belong to any class.

Let's look at these pieces of code:

import java.util.ArrayList;

public class LambdaExpression {

    public static void main(String[] args) {

        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Mango");
        fruits.add("Pear");
        fruits.add("Grapes");

        for(String element : fruits){
            System.out.println(element);
        }
    }
}

Output:

Apple
Mango
Pear
Grapes

External VS Internal iteration

The code above is for external iteration, which works fine, except that:

  1. The loop is sequential, and it has to go through all the elements, and they will be printed in the order specified by the collection.

  2. The control flow of this program is rigid. For example, if we want to avoid printing “apple”, we will have to add an if statement and, therefore, more code.

So, what can you do instead? This is where a lambda expression can be of great help. You get the same output if you use a lambda expression instead of the for-each loop.

import java.util.ArrayList;

public class LambdaExpression {

   public static void main(String[] args) {

       ArrayList<String> fruits = new ArrayList<>();
       fruits.add("Apple");
       fruits.add("Mango");
       fruits.add("Pear");
       fruits.add("Grapes");

       fruits.forEach((n) -> System.out.println(n));
   }
}

The syntax for a lambda expression is parameter → expression. The iteration is performed in the background. The code is more readable and performant.

Lambda expression within a functional interface

Lambda expressions are used by functional interfaces (an interface that only has a single abstract method). In case you don’t want to use lambda expressions, create an anonymous class (a class that doesn’t have a name and for which an object is created. In this case, you created a FunctionalInterface object).

public interface FunctionalInterface {

    public void showFunctionalInterface();
}

public class FunctionalInterfaceExample {

   public static void main(String[] args) {
       String message = "You are learning lambda expression!";

       //Anonymous class
       FunctionalInterface functionalInterface = new FunctionalInterface() {
           @Override
           public void showFunctionalInterface() {
               System.out.println(message + " Well done!");
           };

       };
       functionalInterface.showFunctionalInterface();
   }
}

If, instead, you want to use a lambda expression, then this is what you could do:

@java.lang.FunctionalInterface
interface FunctionalInterface {

    public void showFunctionalInterface();
}
public class FunctionalInterfaceExample {

    public static void main(String[] args) {

        String message = "You are learning lambda expression!";

        FunctionalInterface functionalInterface = ()-> System.out.println(message + " Well done!");
        functionalInterface.showFunctionalInterface();

    }
}

The outcome is:

You are learning lambda expression! Well done!

Benefits of using a lambda expression

In conclusion, the benefits of using a lambda expression are:

  • Cleaner code

  • Eliminates boilerplate code

  • Better performance

  • Facilitates functional programming

I hope you've found this helpful. Let me know of any feedback!

Did you find this article valuable?

Support Maddy by becoming a sponsor. Any amount is appreciated!