Java-functional programming and lambda expressions

Source: Internet
Author: User

1. The most important new features of Java 8

Lambda expressions, interface improvements (default methods), and batch data processing.

2. Function-Type programming

In essence, programming focuses on two dimensions: operations on data and data.

Object-oriented programming generics emphasize having operations around data, which allows for reuse in class, and when new data types are added to a class, the original code does not need to be modified.

Functional programming is a different programming model that centers on operations (functions) and emphasizes variable invariance. The guideline for functional programming is to not rely on external data or to change the value of external data. This feature satisfies the requirements of multi-core parallel programming and therefore simplifies parallel program development.

Functional programming uses functions to express all the concepts and to do all the work. In object-oriented programming, objects are passed as parameters, whereas in functional programming, functions can be passed as arguments to other functions, and return values can also be functions.

From the developer's point of view, functional programming does not support assignment operations, and the execution of a function returns only a value or function, without any side effects, so it appears that a function is a large expression.

3, lambda expression use occasions

Lambda expressions implement functional programming that allows developers to use program code as if it were a data type. A method can be passed as a parameter to another method, as if it were an object instance or number.

Use occasions: Usually when a function is needed, but does not want to bother to name a function, that is, the anonymous function.

A lambda expression can be used to replace a widely used internal anonymous class implementation callback function for an event responder.

4. Lambda expression syntax

A lambda expression is an anonymous function object that can be passed as a parameter without a name, a parameter list, a function body, a return type, or an exception. Its type is the function interface (functional Interface).

Grammar:

1) Formal parameters: The parameter list, which is the parameter of the method in the function-type interface.

2) Right arrow (-): Split function.

3) method body. An expression or block of code is an implementation of a method in a function-type interface. (return value can be returned)

  

5. What is a functional interface

A functional interface refers to an interface that contains only one abstract method.

Java.lang.Runnable, Java.util.Comparator is a typical functional interface.

  

6. Connection of functional interfaces and lambda expressions

A lambda expression requires a functional interface as its corresponding type, and its method body is the implementation of the function interface. Each lambda expression of that interface type is matched to the abstract method of that interface.

7. How to create an object with a functional interface

    • standard method creation;
    • Created with a lambda expression. (Can simplify code)
      /* lambda expression creates an object of a functional interface */ Interface converter{    Integer convert (String from); Converter Converter=(from)->integer.valueof (from); integer integer=converter.convert ("123");

8. Auto type push to

In the above code, the compiler knows that converter has only one method, convert (), so the convert () method definitely corresponds to the expression (from)->integer.valueof (from);

Since convert () has only one parameter, the from must be of type string.

9. Method reference

What exactly does a lambda expression mean? Any lambda expression can be treated as an anonymous descriptor for the only abstract method in a function interface.

You can use a specific method of a class to represent this descriptor, which is a method reference. This allows the instance to be passed directly as an argument to a functional interface without having to refer to an instance with a bound method.

A reference to a method is implemented by: symbol, which is considered to be the same as a lambda expression and can be used where the function interface applies.

/* equivalent to the method of making the converter interface equivalent to integer::valueof () */ Converter Converter=integer::valueof (); integer integer=converter.convert ("123");

  

10. New feature-default method for interface

The default method of the interface is to solve the problem of interface evolution, in which the interface is modified in the new version, causing the earlier versions of the code to fail to run. Because the methods in the interface must be implemented, a lot of refactoring can occur if new methods are added to the interface. Therefore, if you add a new method to an interface, you can provide the default implementation of the method.

With the default method, the code can continue to run for existing interface users. The new code can continue to use the method, or you can override the default implementation.

Interface formula{    double calculate (int  a); /* New method that provides a default implementation */     default double sqrt (int  a) {    return  math.sqrt (s);    }}

Java-functional programming and lambda expressions

Related Article

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.