Java Lambda expressions

23 May 2014

Concept of Lambda expressions

Lambda expressions enable you to treat functionality as method argument, or code as data. in this post we're using an example of using Lambda expression in order to define a standard functional Interface (in this case ActionListener) with an anonymous class.



Sample Code

In a typical java Swing application (Java desktop GUI based app), in previous versions you would attach listeners to the GUI components on your screen using anonymous classes or other approaches:

 btn.setOnAction(new EventHandler<ActionEvent>() {

        <span class="nd">@Override</span>
        <span class="kd">public</span> <span class="kt">void</span> <span class="nf">handle</span><span class="o">(</span><span class="n">ActionEvent</span> <span class="n">event</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Hello World!"</span><span class="o">);</span>
        <span class="o">}</span>
    <span class="o">});</span></code></pre></figure>

with java Lambda Expressions you can reduce the ammount of code used to describe the Listener implementation.

btn.setOnAction(
          event -> System.out.println("Hello World!")
        );

notice how the lambda expression only declares the most important part of the anonymous class which is the actual method implementation itself.



Syntax of Lambda Expressions

A lambda expression consists of the following:

A comma-separated list of formal parameters enclosed in parentheses. The CheckPerson.test method contains one parameter, p, which represents an instance of the Person class.

Note: You can omit the data type of the parameters in a lambda expression. In addition, you can omit the parentheses if there is only one parameter. For example, the following lambda expression is also valid:

p -> p.getGender() == Person.Sex.MALE
    && p.getAge() >= 18
    && p.getAge() <= 25

The arrow token, ->

A body, which consists of a single expression or a statement block. This example uses the following expression:

p.getGender() == Person.Sex.MALE
    && p.getAge() >= 18
    && p.getAge() <= 25

If you specify a single expression, then the Java runtime evaluates the expression and then returns its value. Alternatively, you can use a return statement:

p -> {
    return p.getGender() == Person.Sex.MALE
        && p.getAge() >= 18
        && p.getAge() <= 25;
}

A return statement is not an expression; in a lambda expression, you must enclose statements in braces ({}). However, you do not have to enclose a void method invocation in braces. For example, the following is a valid lambda expression:

email -> System.out.println(email)

Note that a lambda expression looks a lot like a method declaration; you can consider lambda expressions as anonymous methods—methods without a name.



Additional links:

Oracle java tutorial

comments powered by Disqus