Getting Started with lambda expressions for new features of JAVA8

Source: Internet
Author: User
Tags instance method

What is a lambda expression
  Lambda expressions: You can make your code more concise. Ambda cannot appear alone, it requires a function interface to be put in place, it can be said that the lambda expression method body is the implementation of a functional interface, lambda instantiation function interface, you can use the function as a method parameter, or treat the code as data.

Two lambda expression properties
First we need to know what the functional interface is.
Functional interface (functional Interfaces): If an interface defines a unique abstract method, then this interface becomes a functional interface. At the same time, a new annotation was introduced: @FunctionalInterface. Can put him in front of an interface, indicating that this interface is a functional interface. This annotation is not necessary, as long as the interface contains only one method interface, the virtual opportunity is automatically judged, but it is best to use annotations @FunctionalInterface on the interface to declare. A @FunctionalInterface interface is added to the interface, only one abstract method is allowed, or the compiler will error.
The syntax format for lambda expressions:

The 1.LAMBDA expression uses () to indicate that there are no parameters.
2. If only one parameter is included in the lambda expression, omit ().
3. If the subject of a lambda expression is a block of code, you need to use {}, which is no different from the normal Java code block, or can return or throw an exception.
4. If the type of the parameter can be inferred by the compiler can omit the parameter type, of course you can also add.
5. Optional return Keyword: If the principal has only one expression return value, the compiler automatically returns the value, and the curly brace needs to specify that the expression returns a numeric value.

Common wording:

(a) A * a
(int a, int b), A + b
(A, B), {return a-A;}
() System.out.println (Thread.CurrentThread (). GetId ())

Three. Method references
  
In a lambda expression, a method reference is a simplified notation, and the method referenced is the implementation of the method body of the lambda expression
Grammatical structure: objectref:: MethodName
The left is the class name or instance name, the middle "::" is the method reference symbol, the right is the corresponding method name
Method references generally fall into three categories:
static method Reference, instance method reference, constructor method reference

Four. Serial stream operation

What is Stream? The stream here differs from the IO InputStream and Outputstream,stream in the package Java.util.stream, which is also a new addition to Java 8, and the stream is only a set of elements that support serial parallel aggregation operations, which can Understood as an enhanced version of a collection or iterator. What is an aggregation operation? For simple examples, there are average, maximum, minimum, sum, sort, filter, and so on.

Several characteristics of Stream
One-time processing. At the end of one processing, the current stream is closed.
Supports parallel operations
Common ways to get a Stream
Get from the collection
Collection.stream ();
Collection.parallelstream ();

  Stream This feature is very practical, in the normal Serviceimpl can be easily processed by SQL data processing without the need to manually write a lot of logic, equivalent to MyBatis and MySQL operation in the service layer.

Five. Actual combat

  At present the project is just used for java8, can try to use in the project first, slowly groping. Waiting to be updated .....

Getting Started with lambda expressions for new features of JAVA8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.