Lambda expressions Introduction

JDK1.8 added Lambda expressions, add a really functional programming features for Java.

Lambda expressions are the type of function. But in Java, the Lambda expressions is like , they must be attached to a particular class of object types - functional interface.

Lambda expressions biggest feature is: What does it do, rather than how to do it.

Functional Interface

for example:

Defining an interface, and add annotations @FunctionalInterface, stated as a functional interface, which takes no arguments and returns a result of type String

@FunctionalInterface
public interface MyInterface {
    String test();
    default String getMe() {
        return "This is me";
    }
}

Test interface, printing 1-10 

public class MyTest {
    public String testMyInterface(MyInterface myInterface) {
        return "MyInterface accumulate result is: " + myInterface.test();
    }
    public static void main(String[] args) {
        MyTest myTest = new MyTest();
        String string = myTest.testMyInterface(() -> {
            String str = "";
            for (int i = 1; i < 10; i++) str += i + ",";
            return str;
        });
        System.out.println(string);
    }
}

Statement on functional interface has the following note points

  • A functional interface has exactly one abstract methods, static methods and the default method does not count
  • If the abstract method declarations abstract method in a public class java.lang.Object overridden method in the interface, then the function is not included in the abstract method in the interface
    • The reason: the interface is always directly or indirectly inherited from class java.lang.Object
  • Examples of functional interfaces can be referenced by a lambda expression, methods, or create a reference to the constructor
  • When the following condition is not satisfied, the compiler error
    • Type is the interface type, rather than notes, class or enumeration
    • Annotated interface fulfills the requirements of the above-described function of the interface
  • If an interface only an abstract method, but did not give the interface declaration FunctionInterface annotation, the compiler will still be seen as a function of the Interface interface
  • Address functional interface implementation class will bring lambda

java.util.function package several major interface

  • Function
    • Receiving a parameter and returns a result
    • Get the results from the method: apply
  • Consumer
    • Receiving a parameter, does not return results
    • Call the method: accept
  • Predicate
    • Receiving a parameter and returns a boolean result
    • The method of calculation results: test
  • Supplier
    • No arguments, returns a result
    • It can be used for factory use
    • Production methods: get
  • java.util.function also provides various modifications of the above method
  • Further, in order to improve the efficiency, but also for primitive types (int, double, long) to provide a corresponding interface

 

Guess you like

Origin www.cnblogs.com/flying-snake/p/11516587.html