Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java Lambda syntax


May 10, 2021 Java


Table of contents


Java Lambda syntax


The general syntax of using lambda expressions is

(Parameters) -> { Body }

-> Separates the argument from the lambda expression body.

Arguments are enclosed in parentheses, the same as methods, while lambda expression body is a block of code enclosed in braces.

Attention

Lambda expression body can have local variables, statements. W e can use break, continue, and return in the lambda expression body. We can even throw an exception from the body of the lambda expression.

The lambda expression has no name because it represents an anonymous internal class.

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

Lambda expressions cannot have the throws clause like methods.

Lambda expressions cannot be generics, which are defined in function interfaces.


Explicit and implicit lambda expressions

Lambda expressions that do not declare their parameter types are called implicit lambda expressions.

An explicit lambda expression is a lambda expression that declares the type of its arguments.

The compiler infers the type of argument used for implicit lambda expressions

Example

The following code uses a single method to create an interface and use it as a lambda expression type. W hen creating a lambda expression, we declare the type of argument s1 to be the Integer type.

public class Main {

  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 produces the following results.

Java Lambda syntax

Example 2

There is no demonstration of the type used here. When the type is ignored, the compiler must calculate it.

public class Main {

  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 produces the following results.

Java Lambda syntax

The argument type is omitted

We can choose to omit the type of argument in the lambda expression.

In lambda (int x, int y) -> { return x + y; } of argument declared.

We can safely rewrite lambda expressions to omit parameter types

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

If we choose to omit the parameter type, we must omit the type of 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);

  }
}

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

The code above produces the following results.

Java Lambda syntax

Single parameter

For a single parameter lambda expression, we can omit parentheses because we omit the argument type.

Lambda expression (String msg) -> {System.out.println(msg);}
There's everything.

Then we can omit the parameter type

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

We can further omit the parameter types and parentheses, as shown below.

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);

  }
}

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

The code above produces the following results.

Java Lambda syntax

No arguments

For lambda expressions without parameters, we still need parentheses.

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

The following example shows how to use BooleanSupplier

import java.util.function.BooleanSupplier;

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 produces the following results.

Java Lambda syntax

Final modifier

You can use the final modifier for the expression lambda expression final the argument declaration.

The following lambda expression uses the final modifier.

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

We can use only 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);

  }
}

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

The code above produces the following results.

Java Lambda syntax

Lambda expression body

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

Block statements are enclosed in braces, while a single expression can be free of braces.

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

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

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

The following lambda uses an expression:

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

Expressions do not require braces.

Lambda does not have to return a value. The following two lambda expressions simply output the arguments to the standard output and do not return anything.

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

Cases:

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 produces the following results.

Java Lambda syntax