Functional Programming With Java 简明教程

Functional Programming - Lambda Expressions

Java 8 中引入了 lambda 表达式,声称是 Java 8 中最大的功能。lambda 表达式促进了函数式编程,极大地简化了开发。

Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java 8. Lambda expression facilitates functional programming, and simplifies the development a lot.

Syntax

Lambda 表达式的特征在于以下语法。

A lambda expression is characterized by the following syntax.

parameter -> expression body

以下是 Lambda 表达式的几个重要特征。

Following are the important characteristics of a lambda expression.

  1. Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.

  2. Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.

  3. Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.

  4. Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.

Lambda Expressions Example

在任意编辑器中创建以下 Java 程序,例如,在 C:\> JAVA 中创建。

Create the following Java program using any editor of your choice in, say, C:\> JAVA.

Java8Tester.java

public class Java8Tester {

   public static void main(String args[]) {
      Java8Tester tester = new Java8Tester();

      //with type declaration
      MathOperation addition = (int a, int b) -> a + b;

      //with out type declaration
      MathOperation subtraction = (a, b) -> a - b;

      //with return statement along with curly braces
      MathOperation multiplication = (int a, int b) -> { return a * b; };

      //without return statement and without curly braces
      MathOperation division = (int a, int b) -> a / b;

      System.out.println("10 + 5 = " + tester.operate(10, 5, addition));
      System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
      System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
      System.out.println("10 / 5 = " + tester.operate(10, 5, division));

      //without parenthesis
      GreetingService greetService1 = message ->
      System.out.println("Hello " + message);

      //with parenthesis
      GreetingService greetService2 = (message) ->
      System.out.println("Hello " + message);

      greetService1.sayMessage("Mahesh");
      greetService2.sayMessage("Suresh");
   }

   interface MathOperation {
      int operation(int a, int b);
   }

   interface GreetingService {
      void sayMessage(String message);
   }

   private int operate(int a, int b, MathOperation mathOperation) {
      return mathOperation.operation(a, b);
   }
}

Verify the Result

按照如下方式使用 javac 编译器编译类 −

Compile the class using javac compiler as follows −

C:\JAVA>javac Java8Tester.java

现在按照如下方式运行 Java8Tester −

Now run the Java8Tester as follows −

C:\JAVA>java Java8Tester

它应该产生以下输出 −

It should produce the following output −

10 + 5 = 15
10 - 5 = 5
10 x 5 = 50
10 / 5 = 2
Hello Mahesh
Hello Suresh

以下是在上面的示例中需要考虑的几个重要事项。

Following are the important points to be considered in the above example.

  1. Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. In the above example, we’ve used various types of lambda expressions to define the operation method of MathOperation interface. Then we have defined the implementation of sayMessage of GreetingService.

  2. Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.

Scope

使用 lambda 表达式,你可以引用任何 final 变量或有效 final 变量(只分配一次)。如果变量第二次分配了值,lambda 表达式将抛出一个编译错误。

Using lambda expression, you can refer to any final variable or effectively final variable (which is assigned only once). Lambda expression throws a compilation error, if a variable is assigned a value the second time.

Scope Example

在任意编辑器中创建以下 Java 程序,例如,在 C:\> JAVA 中创建。

Create the following Java program using any editor of your choice in, say, C:\> JAVA.

Java8Tester.java

public class Java8Tester {

   final static String salutation = "Hello! ";

   public static void main(String args[]) {
      GreetingService greetService1 = message ->
      System.out.println(salutation + message);
      greetService1.sayMessage("Mahesh");
   }

   interface GreetingService {
      void sayMessage(String message);
   }
}

Verify the Result

按照如下方式使用 javac 编译器编译类 −

Compile the class using javac compiler as follows −

C:\JAVA>javac Java8Tester.java

现在按照如下方式运行 Java8Tester −

Now run the Java8Tester as follows −

C:\JAVA>java Java8Tester

它应该产生以下输出 −

It should produce the following output −

Hello! Mahesh