DEV Community

Cover image for Lambda Expression Simplified
omoluabidotcom
omoluabidotcom

Posted on

Lambda Expression Simplified

In the release of Java 8, the Lambda Expression was introduced. It's a technique for writing clear and succinct code. One of those concepts that people, especially novices, find challenging to understand is Lambda Expression. You will learn about Lambda expressions in this article, including their syntax, uses, and justifications.

Overview of the Lambda Expression

To better grasp the vocabulary and concepts in this article, I do advise you to have some familiarity with Java before reading it, especially basic knowledge of the Interface.

Java is an object-oriented programming language. However, the introduction of Lambda Expression in Java enabled us to write Java code using the functional programming paradigm.

The following two factors play a vital role in the existence of Lambda Expressions:

  1. Introduce Functional Programming in Java.
  2. Improve clear and concise coding in Java

The Java interface

Java's concept of interfaces is that they represent a contract between a class and the particular interface that has been implemented, requiring that the class supply its own implementation of the interface's methods. Example below

// Interface Animal
public interface Animal {

    public String talking();
}

// Class Sheep with implementation
public class Sheep implements Animal{

    public String talking() {

        return "Meaeeeeeeeee";
  }
}

Enter fullscreen mode Exit fullscreen mode

Now, in order to call this method, we must first create an object of the class Sheep and then use that object to call the method as shown in the sample code below.


// Sheep object
Sheep sheepObj = new Sheep(); 

// calling talking method
sheepObj.talking();

Enter fullscreen mode Exit fullscreen mode

A different approach is to write a method sheepTalking and make the argument to be passed of type Animal.


public String sheepTalking(Animal animalTalk) {

    return animalTalk.talking();
}


Enter fullscreen mode Exit fullscreen mode

Therefore, this method accepts any object from a class that implements the interface, as seen in the example below.


// Sheep object
Sheep sheepObj = new Sheep(); 

// Sheep object is passed as parameter
sheepObj.sheepTalking(sheepObj);

Enter fullscreen mode Exit fullscreen mode

In essence, the way the method that accepts Sheep class objects is written, it appears as though we are providing the method's implementation directly into another method as an argument like so.


// Depict how it look
  sheepObj.sheepTalking(
            public String talking() {

                return "Meaeeeeeeeee";
        }
        );
Enter fullscreen mode Exit fullscreen mode

Lambda Expressions Syntax

Lambda expression is the main topic of our attention. The implementation of a method can be passed in as an argument using lambda expressions. The process is similar to what we did above but with a different syntax we provide the brace for argument and add a dash and greater than sign before the curly brackets as shown; the access modifier, return type, and the method name is not given.


// Lambda Expression
sheepObj.sheepTalking( () -> {
             return "Meaeeeeeeeee";
            }
        );

Enter fullscreen mode Exit fullscreen mode

As you can see, the syntax has been changed to remove the access modifier, omit the return type, and leave out the method name; that’s how to write lambda in Java.

*Functional Interface *

Functional interfaces are interfaces with a single abstract method. Single Abstract Method Interface, or SAM Interface, is another name for them. See sample code for illustration.


@FunctionalInterface
public interface Animal {

    public String talking();
}

Enter fullscreen mode Exit fullscreen mode

The term "abstract method" refers to a method without defined implementation in the interface. As a result, a functional interface is one that has just one abstract method. Lambda Expression is only compatible with Functional interface any other Interface can't be use with Lambda Expression.

Although not required, the annotation @FuntionalInterface is advised, particularly in large projects to alert other developers working on the codebase to prevent updates that might break the code.
You can design your own functional interfaces, but Java already has several built-in, so it's best to verify before writing any custom ones.

Using the built-in SAM interface for Java is outside the scope of this article, but the Java.until.function package includes built-in Functional Interfaces for Java. I'll talk about this package and discuss the more popular interfaces in a later article.

Summary

  1. To pass a behavior or method as a parameter, we use a lambda expression.

  2. The syntax for lambda expressions

  • No access modifier
  • Omit return type
  • Method name should not be added
  1. Lambda Expression makes our code short, concise, and readable.

Please use the comment area if you have any questions. Connect with me on Twitter and LinkedIn.
Have a good day and thanks for your time.

Credit

Java 8 documentation

Top comments (0)