Home  >  Article  >  Java  >  Introduction to lambda expression syntax in Java8

Introduction to lambda expression syntax in Java8

黄舟
黄舟Original
2017-09-19 10:39:381573browse

As we all know, lambda expression is a new feature provided in JAVA8. It supports Java and can also perform simple "functional programming". The following article mainly introduces you to the relevant information about the lambda expression syntax of the Java8 learning tutorial. Friends in need can refer to it.

Preface

I believe everyone knows that lambda expressions were introduced in Java8. From the perspective of behavioral parameterization, when using, Using behaviors as parameters eliminates unnecessary class declarations surrounding the outer layer, making the code more concise.

The syntax of lambda expression

lambda expression consists of three parts: parameters, ->, and function body. In fact, the function body can be an expression or a statement. Statements should be enclosed in {}, but expressions cannot.

lambda expression example


(List<String> list) -> list.isEmpty() // 布尔类型表达式
() -> new Apple(10) // 创建一个新对象
(Apple a) -> { System.out.println(a.getWeight()); } // 使用一个对象的属性
(String s) -> s.length() // 选择或提取一个对象的属性
(int a, int b) -> a * b // 组合两个参数
(Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()) // 比较两个对象

Behavior parameterization

As you can see, lambda expressions focus on expressing behavior. In fact, before Java8, there were already similar examples of processing behaviors as parameters:


// java.util.Comparator
public interface Comparator<T> {
 public int compare(T o1, T o2);
}


inventory.sort(new Comparator<Apple>() {
 public int compare(Apple a1, Apple a2){
 return a1.getWeight().compareTo(a2.getWeight());
 }
});

In the above example, A Comparator interface is defined, and the compare method is defined. List type inventory defines the sort method, which takes a Comparator as a parameter. When used, an anonymous or non-anonymous Comparator object is generated, which implements the compare method, specifies the specific comparison behavior, and then passes the Comparator object as a parameter to the sort method. The actual function is to pass the comparison behavior as a parameter to the sort method. This idea and approach is called "behavior parameterization". A used method (such as the sort method) can correspond to multiple behaviors. When the behavior needs to be changed, only the code representing the behavior needs to be modified, and the user does not need to make modifications, which increases the robustness of the code.

Anonymous inner class

As in the above example, a Comparator type object is directly new without defining a variable to save the object. Reference is called using anonymous inner classes. In the case of anonymous inner classes, the redundancy of the code can be seen more clearly since methods cannot be reused. Because you need to manually new an object every time you use it, and you also have to write the signature of the compare method, but the only thing that is really useful is the body of the compare method. In Java 8, these codes can be simplified using lambda expressions. For example, the use of the above sort method can be simplified to:


inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight() ) );

Example of defining a thread:


Thread t = new Thread(new Runnable() {
 public void run(){
 System.out.println("Hello world");
 }
});

can be simplified to:


Thread t = new Thread(() -> System.out.println("Hello world"));

Example of click event in GUI:


Button button = new Button("Send");
button.setOnAction(new EventHandler<ActionEvent>() {
 public void handle(ActionEvent event) {
  label.setText("Sent!!");
 }
 });

can be simplified to:


Button button = new Button("Send");
button.setOnAction((ActionEvent event) -> label.setText("Sent!!"));

It can be seen that lambda expressions are more concise and can also make development Readers can focus more on the defined behavior when reading and writing code.

Summary

This article introduces the syntax of lambda expressions, introduces the usage scenarios of lambda expressions, and uses lambda expressions. benefit. In the next article, we will explain how to define and use lambda expressions, as well as the special specifications of lambda expressions in Java compared to other languages.

Summarize

The above is the detailed content of Introduction to lambda expression syntax in Java8. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn