Learning Java 8 Lambda Expressions

In this tutorial we will show how to create and start an embedded Undertow Web server using JDK 1.8 and Lambda expressions. 

First of all you make sure you have a JDK 1.8 compatible. If not download it from Oracle site: http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html


Next you need an Eclipse environment compatible with JDK 1.8. You have several options:
1) Download Eclipse Kepler (4.3.2) SR2 with Javaâ„¢ 8 Patches (or the latest milestone of Eclipse Luna): See this link https://www.eclipse.org/downloads/index-java8.php
2) Patch your older Eclipse Kepler 4.3 installation as indicated in Eclipse wiki (https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler)  

Once done with Eclipse a small tweak to make Eclipse work with JDK 1.8: from Eclipse Menu, select Windows > Preferences and then select Java > Installed JREs. Select Java > Compiler and set Compiler compliance level to 1.8, as shown in the picture.

lambda expressions jdk 1.8 java 8 tutorial

Finally, if you are using Maven for compiling, make sure you set in your pom.xml the maven.compiler.target and maven.compiler.source to use JDK 1.8:

<properties>
      <maven.compiler.target>1.8</maven.compiler.target>
      <maven.compiler.source>1.8</maven.compiler.source>
   </properties>

And now finally the code. In this tutorial we will show how to turn our Hello world Undertow Web server into a Java 8 Web server using lambda expressions with just 5 rows of code!

Coding Lambda Expressions

What are lambda expressions? lambda expressions are one of the most exciting features of Java 8; if you have been working with functional programming and with the concept of closures, it won’t be a new concept for you.

In Java, a lambda expression provides a way to create an anonymous function by introducing a new Java type: the anonymous function type that can then be passed as an argument or returned from methods; in a nutshell, it is a method without a statement, a kind of shortcut that allows you to write a method in the same place where you need them.

The lambda expressions are particularly useful if you need to define a short function with few lines of code that will not be used across other parts of your code. This is the case for example of actionListeners that are attached to UI components. In these cases you will write just the action part that is fired and not the part with modifier, name, etc. making the code clearer and less wordy.
Let’s see a concrete example. Here is the Undertow Hello World that we have discussed in this tutorial.

public class App  {
    public static void main(final String[] args) {
        Undertow server = Undertow.builder().addHttpListener(8080, "localhost")
                .setHandler(new HttpHandler() {

                    public void handleRequest(final HttpServerExchange exchange)
                            throws Exception {
                        
                        exchange.getResponseSender().send("Hello World");
                    }
                }).build();
        server.start();
    }
}   

Now let’s rewrite it so that is uses a Lambda expression:


public class AppLambda {
    public static void main(final String[] args) {
        Undertow server = Undertow.builder().addHttpListener(8080, "localhost")
                .setHandler(exchange -> {
                    exchange.getResponseSender().send("Hello World");
                }).build();

        server.start();
    }

}

Much nicer isn’t it? notice that there is no new HttpHandler() and no public void handleRequest(HttpServerExchange exchange). Here the compiler understands that there is only one method in the HttpHandler interface and that takes only one parameter. So obviously, it decides (finalizes) that the exchange is a reference to HttpServerExchange. The code written in the setHandler(ActionEvent) is enclosed as usual in the curly braces.