Java Lambda Syntax








The general syntax for using lambda expressions is

(Parameters) -> { Body }

-> separates parameters and lambda expression body.

The parameters are enclosed in parentheses which is the same way as for methods while the lambda expression body is a block of code enclosed in braces.

Note

The lambda expression body may have local variables, statements. We can use break, continue, and return in lambda expression body. We can even throw exceptions out of lambda expression body.

A lambda expression does not have a name since it represents anonymous inner class.

The return type of a lambda expression is inferred by the compiler.

A lambda expression cannot have a throws clause like a method.

A lambda expression cannot be generic while the generic is defined in the functional interface.





Explicit and implicit lambda expression

A lambda expression which does not declare the its parameters' type is called implicit lambda expression.

An explicit lambda expression is a lambda expression which declares its parameters' type.

The compiler will infer the types of parameters for implicit lambda expression.

Example

The following code creates interface with single method and uses it as lambda expression type. When creating the lambda expression we declare the type of the parameter s1 to have the Integer type.

public class Main {
/*w  w  w. ja  va  2  s .  com*/
  public static void main(String[] args) {
    MyIntegerCalculator myIntegerCalculator = (Integer s1) -> s1 * 2;

    System.out.println("1- Result x2 : " + myIntegerCalculator.calcIt(5));

  }
}
interface MyIntegerCalculator {

  public Integer calcIt(Integer s1);

}

The code above generates the following result.





Example 2

Here is the demo again without using the type. When ignoring the type the compiler has to figure it out.

public class Main {
//  ww w.  ja  v  a  2 s .c o m
  public static void main(String[] args) {
    MyIntegerCalculator myIntegerCalculator = (s1) -> s1 * 2;

    System.out.println("1- Result x2 : " + myIntegerCalculator.calcIt(5));

  }
}
interface MyIntegerCalculator {

  public Integer calcIt(Integer s1);

}

The code above generates the following result.

Omitting Parameter Types

We can choose to omit parameter types in lambda expressions.

In lambda expression (int x, int y) -> { return x + y; } the types of parameters are declared.

We can safely rewrite the lambda expression by omitting the types of parameters as

(x, y) -> { return x + y; }

If we choose to omit the types of parameters, we have to omit types for all parameters.

public class Main {
  public static void main(String[] argv) {
    Processor stringProcessor = (str) -> str.length();
    String name = "Java Lambda";
    int length = stringProcessor.getStringLength(name);
    System.out.println(length);//from   ww w.ja  v a 2  s .  c  om

  }
}

@FunctionalInterface
interface Processor {
  int getStringLength(String str);
}

The code above generates the following result.

Single Parameter

For a single parameter lambda expression we can omit the parentheses as we omit the parameter type.

The lambda expression (String msg) -> {System.out.println(msg);} has everything.

Then we can omit the parameter type to have

(msg)->{System.out.println(msg);}

We can further omit the parameter type and parentheses as follows.

msg -> { System.out.println(msg); }
public class Main {
  public static void main(String[] argv) {
    Processor stringProcessor = str -> str.length();
    String name = "Java Lambda";
    int length = stringProcessor.getStringLength(name);
    System.out.println(length);/*  w  w  w. jav  a2s  .c  o  m*/

  }
}

@FunctionalInterface
interface Processor {
  int getStringLength(String str);
}

The code above generates the following result.

No Parameters

For a lambda expression with no parameters, we still need the parentheses.

() -> { System.out.println("hi"); }

The following example shows how to use BooleanSupplier.

import java.util.function.BooleanSupplier;
//from   w w  w.j  a  va 2s . c  o  m
public class Main {
  public static void main(String[] args) {
    BooleanSupplier bs = () -> true;
    System.out.println(bs.getAsBoolean());

    int x = 0, y= 1;
    bs = () -> x > y;
    System.out.println(bs.getAsBoolean());
  }
}

The code above generates the following result.

final Modifier

You can use final modifier in the parameter declaration for explicit lambda expressions.

The following lambda expressions use final modifier.

(final int x, final int y) -> { return x + y; }

We can just use one modifier as follows.

(int x, final int y) -> {return x + y;}
public class Main {
  public static void main(String[] argv) {
    Processor stringProcessor = (final String str) -> str.length();
    String name = "Java Lambda";
    int length = stringProcessor.getStringLength(name);
    System.out.println(length);/* w w  w . ja  v  a  2s .co m*/

  }
}

@FunctionalInterface
interface Processor {
  int getStringLength(String str);
}

The code above generates the following result.

Lambda Expressions Body

The lambda expression body can be a block statement or a single expression.

A block statement is enclosed in braces while a single expression can exist without braces.

In the block statement we can use return statement to return value.

The following lambda expression uses a block statement and use return statement to return the sum.

(int x, int y) -> { return x + y; }

While the following lambda uses an expression:

(int x, int y) -> x + y

The expression doesn't require the braces.

The lambda is not necessary to return a value. The following two lambda expressions just output the parameter to standard output and don't return anything.

(String msg)->{System.out.println(msg);}// a  block   statement
(String msg)->System.out.println(msg)   //an expression

Example:

public class Main {
  public static void main(String[] argv) {
    Processor stringProcessor = (String str) -> str.length();
    String name = "Java Lambda";
    int length = stringProcessor.getStringLength(name);
    System.out.println(length);// www . j a  va 2  s. co m

  }
}

@FunctionalInterface
interface Processor {
  int getStringLength(String str);
}

The code above generates the following result.