I. SUMMARY of Principles
Lambda expression, which can be substituted for some anonymous inner classes. The primary purpose is to invoke the method in the inner class, and the implementation (override) of the method is determined by the lambda expression.
In general, we may not care about the specific method in the anonymous inner class (the overridden method), but only how the method is overridden (the implementation of the method). Therefore, we can construct an intermediate object (usually an interface, such as Funtion), which has a method that requires the override (for example, the function corresponds to the method of apply).
Second, how to use
In the actual writing, you can write only (passed parameters) and {method implementation}, or only the caller of the implementation process and its method name (separated by a colon).
Function<person, integer> getage = person::getage;//parameter Call getage method Integer age = Getage.apply (p);
Comments:
1. 比如 Function<T,R>That T represents the incoming type, which R represents the return type. For example, an expression person -> person.getAge(); , an incoming parameter is a person return value person.getAge() , and a method reference corresponds to a Person::getAge Function<Person,Integer> type.
2. Using a double colon returns this object, and when you invoke the Apply method in the object, you call the method you passed in.
3. This process is similar to the closure of a function. When you use double colons, your classes and methods are passed in and a function object is generated. The object is called at a later time, at which point it invokes the method you passed in the object.
4. Three ways of comparison
Old waylist<integer> list = Arrays.aslist (1, 2, 3, 4, 5, 6, 7); for (Integer n:list) { System.out.println (n) ;} Lambda expression List.foreach (n--System.out.println (n)),//using:: Lambda expression List.foreach (system.out::p rintln) ;
Third, about the functional interface
1. Java 8 allows an interface implementation method, rather than a simple declaration. You need to use the keyword default
2. The functional interface in JAVA8 can also contain static methods in addition to defining abstract methods.
@FunctionalInterfaceinterface functionalinterfacewithstaticmethod {static int sum (int[] array) {return Arrays.stream ( Array). Reduce ((A, b) a+b). Getasint (); void apply ();}
Note. @FunctionalInterface annotations, used primarily for compile-level error checking , plus this note, when you write an interface that does not conform to the functional interface definition, the compiler will error.
3. In general, the functional interface only defines an abstract method, but the interface eventually has a deterministic class implementation, and the final parent class of the class is object. Therefore, a functional interface can define the public method in object, and the protected method does not.
@FunctionalInterfacepublic interface Objectmethodfunctionalinterface {void count (int i); String toString (); Same to Object.tostringint hashcode (); Same to Object.hashcodeboolean equals (Object obj); Same to Object.Equals}
4. Functional interface in the Util package
java.util.functionSeveral types of functional interfaces are defined and sub-interfaces for the base data type.
- predicate--Pass in a parameter, return a bool result, method is
boolean test(T t)
- Consumer--Pass in a parameter, no return value, pure consumption. Method is
void accept(T t)
- Function<t,r>--Pass in a parameter, return a result, by
R apply(T t)
- Supplier--No parameter passed in, returns a result, method is
T get()
- Unaryoperator-The unary operator inherits Function<t,t>, and the incoming parameter is the same type as the return type.
- Binaryoperator--Two-dollar operator, the same type and return type as the two arguments passed in, inheriting bifunction
Method references in Java 8