Lambda Expressions example in Eclipse

In this section you will learn how to use the Eclipse IDE for running the Lambda Expression example. In this first Lambda Expression example we will show you how to write simple hello world example using new Lambda expressions.

Lambda Expressions example in Eclipse

In this section you will learn how to use the Eclipse IDE for running the Lambda Expression example. In this first Lambda Expression example we will show you how to write simple hello world example using new Lambda expressions.

Lambda Expressions example in Eclipse

Lambda Expression example in Java 8 using Eclipse IDE

Learn how to download the JDK 8 RC build and configure it with Eclipse IDE. You will also learn how to write your first Lambda Expression example in Java 8.

First of all we will show you how you can set-up the development environment using Java 8 RC release software and then configure the Eclipse IDE. We will show you how to download the Eclipse for using the JDK 8 for testing the application.

You will be needing the Eclipse IDE which supports the JDK 8.

First of all download the JDK 8 RC build from https://jdk8.java.net/. Following screen shot shows the link to download the JDK 8 Release Candidate:

Download JDK 8 release candidate

You should download the file by clicking on the above link.

Double click the downloaded file and then install and configure it on your computer. You should configure the JAVA_HOME and PATH variables on your computer.

Now you how to download the JDK 8 supporting eclipse IDE. Following is the URL to download the JDK 8 supporting Eclipse IDE:

Here is the screen shot of the website:

JDK 8 Supporting Eclipse

Download and extract the content of the file. Now click on the "eclipse.exe" to run the eclipse IDE.

Then go to Windows -> Preferences -> Compiler as shown below:

JDK 8 configuration in Eclipse

Here you have to select the Compiler compliance level: 1.8(BETA) for using the JDK 8.

Now we will write simple Hello world Lambda Expression in JDK 8.

Create a project in Eclipse IDE and then create a new class "Test.java" as shown below:

Lambda Expression Hello world example in Java

Now add the following code to create an interface:

interface HelloService {
      String hello(String firstname, String lastname);
}

We will use the Lambda Expression. Here is the complete code:

package net.roseindia;

public class Hello {
	interface HelloService {
	      String hello(String firstname, String lastname);
	   }
	
	public static void main(String[] args) {
		HelloService helloService = (String firstname, String lastname) -> {
			String hello = "Hello " + firstname + " " + lastname;
			return hello;
		};
		System.out.println(helloService.hello("Deepak", "Kumar"));	
	}
}

If you run the above code it will print "Hello Deepak Kumar" message on the console.

Here is example code of the Lambda Expression:

HelloService helloService = (String firstname, String lastname) -> {
			String hello = "Hello " + firstname + " " + lastname;
			return hello;
		};

Syntax of Lambda Expression:

Following is the syntax of the Lambda Expression:

(parameters) ->{ expression/statements }

In the above code (String firstname, String lastname) is the parameter list followed by the "->" sign which is used for Lambda Expression. And the statements are enclosed in the {} brackets.

Check more tutorials of Lambda Expression in Java.