Java 8 Lambda Expressions Example

This tutorial highlights the benefits and method of using the Lambda expression which is new Java language feature introduced from Java 8. Lambda expressions are used for executing the methods in the functional interfaces. These are very powerful expressions to reduce the code size in the programming languages. But, it is not yet added as part of the Java language. With Java 8 release, Java programmers are delighted to use this feature.

One of the challenge in the Java code is the instantiate the anonymous classes. The code for using the anonymous class is not very clear and it requires many lines of code. If you look at Java API, there are many interfaces declared with single abstract methods where the implementation done by anonymous way. These type of interfaces are known as the Functional Interfaces.

What is Functional Interface?

With Java 8, a new term “functional interface” is introduced. If an interface contains only one abstract method, then it is known as the functional interface. Prior to Java 8, these interfaces are called as “single abstract method interfaces”. Lambda expression simplifies the way we use the functional interfaces. The following are the few important points about the functional interfaces.

  • Functional interface has only one abstract method
  • @FunctionalInterface annotation can be used for denoting the functional interface. However, it is not necessary to use this annotation, it is only useful to catch the error in the compile time. When you annotate an interface with @FunctionalInterface, if more then one abstract method is defined it will throw a compiler error.
  • Functional interfaces can define one or more default methods.

Lambda Example

FunctionalInterface1.java

package javabeat.net.core.java8;
@FunctionalInterface
public interface FunctionalInterface1 {
	void print();
}

FunctionalInterface2.java

package javabeat.net.core.java8;
@FunctionalInterface
public interface FunctionalInterface2 {
	void print(int i);
}

Java8LambdaExample.java

If you look at the below code example, the above two functional interfaces are used with the lambda expressions. There are two types of lambda:

  1. Lambda statement : It is a single line expression with out open and close braces.
  2. Lambda Block : It is multiple lines of statements enclosed by the braces.
package javabeat.net.core.java8;

/**
 * Java 8 Lambda Example
 * @author krishna
 *
 */
public class Java8LambdaExample {
	public static void main(String args[]){
		// Prior to Java 8 Anonymous class instantiation
		new FunctionalInterface1() {
			@Override
			public void print() {
				System.out.println("Prior to Java 8 Anonymous class instantiation");
			}
		};

		// Lambda Expression where no argument in method
		FunctionalInterface1 f1 = () -> System.out.println("Lambda Expression with No Method Arguements");

		// Lambda Expression where one argument in method
		FunctionalInterface2 f2 = (int i) -> {
			System.out.println("Lambda Block with Method Arguements");
			System.out.println("Parameter Passed : "+ i);
			};

		f1.print();
		f2.print(10);
	}
}

Output

Lambda Expression with No Method Arguements
Lambda Block with Method Arguements
Parameter Passed : 10

Runnable Lambda

This example shows how to use the Runnable interface as the lambda. Runnable interface has only one abstract method run(), which implies that this is a functional interface. Lets look at the example.
Java8LambdaRunnable.java

package javabeat.net.core.java8;

/**
 * Lambda Runnable Example
 * @author krishna
 *
 */
public class Java8LambdaRunnable {
	public static void main(String args[]) {
		System.out.println("=== RunnableTest ===");
		Runnable r1 = new Runnable() {
			@Override
			public void run() {
				System.out.println("Hello world one!");
			}
		};
		Runnable r2 = () -> System.out.println("Hello world two!");
		r1.run();
		r2.run();
	}
}

Output

=== RunnableTest ===
Hello world one!
Hello world two!

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*