Java 8: Lambda Functions – Usage & Examples

One of the major new language features in Java 8 is lambda function. In fact, it is one of the biggest changes since Java 1 release. Lambdas are widely used in the programming language world including the languages that compile to the Java platform. For instance, Groovy language compiles to the Java platform and has a very good support for lambda functions (also known as closures). Oracle decided to bring lambdas to the mainstream language on the JVM-the Java language itself-with Java 8.

LAMBDA FUNCTION RELATED CHANGES IN JAVA 8

Introduction of lambdas required coordinated changes in the language, library, and the VM implementation:

  • The arrow operator (“->”) for defining lambda functions, the double colon operator (“::”) used for method references, and the default keyword
  • The streams library and the integration of the collections library with streams
  • Lambda functions are implemented using the invokedynamic instruction introduced in Java 7

To support introduction of lambdas into the language, the type inference has also been strengthened in Java 8. Lambdas enabled library writers to create parallel algorithms in the library to exploit inherent parallelism in the modern hardware (i.e., multi cores).

In Java 8, java.util has been considerably enhanced using lambda functions. Java 8 has added two new packages java.util.function and java.util.streams.


 

Lambdas can significantly change the way you design and write code. Why? Lambdas support functional programming paradigm – that means learning and using lambdas would mean a paradigm shift to you. But you don’t need to worry about making a major shift—Java seamlessly integrates functional capabilities with the existing object oriented features and you can gradually shift to using more and more functional features in your programs.

Lambda Functions: Syntax

A lambda function consists s of optional parameters, the arrow token, and the body:

LambdaParameters -> LambdaBody

  • LambdaParameters are parameters to the lambda function are passed within opening parenthesis “(” and closing parenthesis “)”. When more than one parameter is passed, they are separated by commas.
  • The arrow operator. To support lambdas, Java has introduced a new operator “->”, also known as lambda operator or arrow operator. This arrow operator is required because we need to syntactically separate the parameter from the body.
  • LambdaBody can be an expression or a block. The body could consist of single statement (in that case no explicit curly braces defining a block are required); such a lambda body is known as “expression lambda.” If there are many statements in a lambda body, they need to be in a block of code; such a lambda body is known as “block lambda.”

Compiler performs type inference for lambda expressions:

  • The compiler infers the type of the parameters if you do not specify the type parameters in a lambda function definition. When you specify the type of parameters, you need to specify all or none; or else you will get a compiler error.
  • You can omit the parenthesis if there is only one parameter. But in this case, you cannot provide the type explicitly. You should leave it to the compiler to infer the type of that single parameter.
  • The return type of the lambda function is inferred from the body. If any of the code in the lambda returns a value, then all the paths should return a value; or else you will get a compiler error.

Some examples of valid lambda expressions (assuming that relevant functional interfaces are available):

  • (int x) -> x + x
  • x -> x % x
  • () -> 7
  • (int arg1, int arg2) -> (arg1 + arg2) / (arg1 – arg2)

Examples of invalid lambda expressions:

  • -> 7

// if no parameters, then empty parenthesis () must be provided

  • (arg1, int arg2) -> arg1 / arg2

// if argument types are provided, then it should be provided

// for all the arguments, or none of them

Lambda Function—An Example

Let us get started with a simple “hello world” example for lambda functions

interface LambdaFunction {
void call();
}

class FirstLambda {
    public static void main(String []args) {
         LambdaFunction lambdaFunction = () -> System.out.println(“Hello world”);
         lambdaFunction.call();
    }
}

When executed, this program prints

Hello world

In this program, the interface LambdaFunction declares an abstract method named call() ; hence it is a functional interface. Inside the main method in FirstLambda class, a lambda function is assigned to a variable of the functional interface type LambdaFunction.

LambdaFunction lambdaFunction = () -> System.out.println(“Hello world”);

Here, the expression () -> System.out.println(“Hello world”) is a lambda expression:

  • The syntax () indicates no parameters.
  • The arrow operator “->” separates method parameters from the lambda body.
  • The statement System.out.println(“Hello world”) is the body of the lambda expression.

How does the lambda expression relate to the functional interface LambdaFunction? It is through the single abstract method inside the LambdaFunction interface: void call(). The signature of this abstract method and the lambda expression must match:

  • The lambda expression has () indicating it has no parameters—it matches with the call method that takes no parameters.
  • The statement System.out.println(“Hello world”) is the body of the lambda expression. This body serves as an implementation of the lambda function.
  • There is no return statement in this lambda expression body and hence the compiler infers the return type of this expression as void type—that matches with the return type of the call method.

The next statement lambdaFunction.call(); invokes the lambda function. As a result of this function call, “Hello world” is printed on the console. Check out this book for more learning about Lambda Functions. You can download the source code here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s