Java 8: Lambda Expression

Maddy - Aug 26 '21 - - Dev Community

This topic can sound a bit complicated and the syntax of a lambda expression can look confusing to some. I hope that by the end of the article you’ll get a clearer idea of what is a lambda expression.

Precisely, we'll look at:

  • What is a lambda expression
  • External VS Internal iteration
  • Lambda expression within a functional interface
  • Benefits of using lambda expression

What is a lambda expression

Java 8 has introduced many new features, and lambda expression is one of them. It’s a function that doesn’t belong to any class. They are called anonymous because they don’t have access modifiers, they don’t have a return type declaration and ultimately they don’t have a name. Usually, methods in Java have all of these characteristics.

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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Apple
Mango
Pear
Grapes
Enter fullscreen mode Exit fullscreen mode

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 we do instead? This is where a lambda expression can be of great help. If instead of the for-each loop we use a lambda expression, we get the same output.

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));
   }
}
Enter fullscreen mode Exit fullscreen mode

The syntax for a lambda expression is parameter → expression. As you can see, 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 we don’t want to use lambda expressions, we can create an anonymous class (a class that doesn’t have a name and for which an object is created. In this case we created a FunctionalInterface object).

public interface FunctionalInterface {

    public void showFunctionalInterface();
}
Enter fullscreen mode Exit fullscreen mode
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();
   }
}
Enter fullscreen mode Exit fullscreen mode

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

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();

    }
}
Enter fullscreen mode Exit fullscreen mode

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! :)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player