Lambda of new features of Java 8 (power node Java College)

Functional interface

Functional interface (functional interface is also called functional interface, which is actually the same thing). In short, a functional interface is an interface that contains only one method. For example, java.lang.runnable and java.util.comparator in the Java standard library are typical functional interfaces. Java 8 provides @ functional interface as an annotation, which is not necessary as long as the interface conforms to functional interface Standard of mouth (i.e. an interface containing only one method), the virtual opportunity automatically judges, but it's best to use the annotation @ functionalinterface on the interface to avoid other team members mistakenly adding new methods to the interface. Lambda in Java cannot appear alone. It needs a functional interface to hold it. The lambda expression method body is actually the implementation of the function interface, Now let's talk about grammar

Lambda grammar

It consists of three parts

1. A formal parameter separated by commas in parentheses. The parameter is the parameter of the method in the functional interface

2. An arrow symbol: - >

3. The method body can be expressions and code blocks. If the method body is a code block, it must be wrapped with {} and a return value is required. With one exception, if the method return value in the functional interface is void, it does not need {}

It looks like this overall

Look at a complete example for easy understanding

As you can see, the code designed with lambda expressions will be more concise and readable.

Method reference

In fact, it is a simplified writing method of lambda expression. The referenced method is actually the method body implementation of lambda expression. The syntax is also very simple. On the left is the container (which can be class name and instance name), in the middle is "::" and on the right is the corresponding method name. It is as follows:

The reference format of the general method is

1. If it is a static method, it is classname:: methodname. For example, object:: equals

2. If it is an instance method, it is instance:: methodname. For example, object obj = new object(); obj::equals;

3. Constructor Classname:: New

Let's take another look at a complete example for easy understanding

It can be seen that dosomething method is the implementation of lambda expression. The advantage is that if you think lambda method has a long experience and affects code readability, method reference is a solution

summary

The above is the whole content of lambda expression syntax. I believe everyone has a certain understanding of lambda expression, but if the code is concise, it will not impress many viewers, and Java 8 will not be so expected. In fact, the introduction of lambda into Java 8 is urgently needed because lambda expression can simplify the multi-threaded or multi-core processing of data on the collection, Provides faster collection processing speed.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>