Introduction to Lambda Expressions

Every time I think of the word Lambda, certain scenes from the movie Revenge of the Nerds just pop right into my head. Lamda expressions are now a part of Java as of version 8 and they are super useful. At first glance they look like some sort of formula for interstellar time travel, but in reality they are pretty straight forward. Stick with this tutorial and by the end you will have a brand new tool in your Java arsenal.

Functional Interface

The first step in understanding Lamda expressions is to learn what a functional interface is. The rule for a functional interface is simple ... it must contain a single abstract method! One and only one abstract method defines a functional interface. On a side note – optionally – a functional interface can contain static and/or default methods, but one and only one abstract method.
A simple example of a functional interface looks like this:
interface FunctionalInterface {
        int mysteryMethod(int x);
// implicitly abstract
}

The Lambda Operator ->

The Lambda operator is basically a dash followed by a greater than sign ->.
Now that we have created a functional interface, we will declare a reference variable of type FunctionalInterface.
FunctionalInterface fi;

Now I am going to create a lambda expression to multiply a value by 3.
fi = (x) -> x * 3;

The statement above may appear confusing so bear with me for just a bit. Let's invoke the mysteryMethod now with an argument of 5.
int i = 0;
i = fi.mysteryMethod(5);
System.out.println(i); // 15

The result is 15, that's cool, but what happened? Let's break this down backwards.
The one and only abstract method mysteryMethod in the FuntionalInterface interface has a method return type of int. That is why the variable i can be assigned the result.
In the portion to the left of the lambda operator (x) -> x * 3, the x will be the argument passed to the mysteryMethod(int x) parameter list.
The x * 3 becomes the on-the-fly method body for the mysteryMethod() and the return value is implicitly the result of the expression.
When you put the whole thing together, our virtual mysteryMethod created with a lambda expression is essentially this:
int mysteryMethod(int x) { return x * 3; }



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md LambaIntro
C:\Java>cd LambaIntro
C:\Java\LambaIntro>Notepad LambaIntro.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class LambaIntro {
    public static void main(String args[]) {
        FunctionalInterface fi;
        fi = (x) -> x * 3; 

        int i = 0;
        i = fi.mysteryMethod(5);
        System.out.println(i); 
        System.out.println(fi.mysteryMethod(38));

        fi = (x) -> x - 7; 
        System.out.println(fi.mysteryMethod(5)); 
        System.out.println();

        AnotherOne ao = (x, y) -> x * y;
        System.out.println(ao.doThings(20, 5));

        ao = (x, y) -> x / y;
        System.out.println(ao.doThings(20, 5));	

        ao = (x, y) -> x + y;
        System.out.println(ao.doThings(20, 5));	

        ao = (x, y) -> x - y;
        System.out.println(ao.doThings(20, 5));	
    }
}

interface FunctionalInterface {
    int mysteryMethod(int x); // implicitly abstract
}

interface AnotherOne {
    int doThings( int x, int y);
}

Now switch back to the command prompt (CMD) and type in javac LambaIntro.java and press Enter.
Now type in java LambaIntro and press Enter.


C:\Java\LambaIntro>javac LambaIntro.java
C:\Java\LambaIntro>java LambaIntro
15
114
-2

100
4
25
15


Final thoughts

Functional interfaces are an essential component of Lamba expressions. A functional interface contains one and only one abstract method.


Tutorials