Java 8 Adds a new lambda expression that can be used instead of a lambda expression when the interface is a @FunctionalInterface
The typical application scenario for a function is: A. 1 interfaces with only 1 methods, avoid writing anonymous classes; B. Methods to accept Fuction interface as parameters
1 Lambda-functional programming features
Functional interface: Functional Interface.
Defines an interface that must have only one abstract method inside the interface, and such an interface becomes a functional interface.
Where a lambda expression can be used, the method declaration must contain a function-style interface.
There are several ways to format functional programming:
A. Standard mode
(Type1 param1, Type2 param2, ..., Typen Paramn), {
Statment1;
Statment2;
//.............
return STATMENTM;
}
B. Omitting types
(Param1,param2, ..., Paramn), {
Statment1;
Statment2;
//.............
return STATMENTM;
}
C. Omit parameter parentheses when parameter is 1
param1, {
Statment1;
Statment2;
//.............
return STATMENTM;
}
D. When there is only one statement, you can omit the statement brace
PARAM1-Statment
2. Method references
If we want to invoke a method that has a name, we can call it directly by its name.
Comparator byname = comparator.comparing (person::getname);
The standard form of a method reference is: Class Name:: Method name. (Note: Just write the method name and do not need to write parentheses)
Type Example
Referencing static methods Containingclass::staticmethodname
An instance method that references an object containingobject::instancemethodname
An instance method that references an arbitrary object of a type Containingtype::methodname
Reference Construction Method Classname::new
Examples of static method references:
String::valueof equivalent to lambda expression (s)-string.valueof (s)
Math::p ow is equivalent to a lambda expression (x, Y)-Math.pow (x, y);
3 Java SE 8 Adds a new package: Java.util.function, which contains commonly used functional interfaces, such as:
Predicate<t>--Receives T object and returns Booleanconsumer<T>--Receives T object, does not return value function<t, r>-- Receive T object, return R object supplier<T>--Provide t object (e.g. factory), do not receive value Unaryoperator<T>--Receive T object, Returnsa T object Binaryoperator <t>--receives two T objects, returns t object
So where the arguments are for these interfaces, we can use the lambda expression directly!
4 Simple examples
The person class has name, age
import Org.apache.commons.lang3.builder.tostringbuilder;import com.huitong.actdemo1.util.PinyinUtil; Public classPerson {PrivateString name; PrivateInteger age; PublicPerson () {} PublicPerson (String name, Integer age) { This. Name =name; This. Age =Age ; } @Override PublicString toString () {returnTostringbuilder.reflectiontostring ( This); } PublicString Getnamepinyin () {returnPinyinutil.topinyin (name); } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; }}
Now for a requirement, a list is sorted by first name pinyin and then by age
Public Static voidMain (string[] args) {List<Person> persons =getpersons (); LongStart =System.currenttimemillis ();//list<person> result = Persons.stream (). Sorted (Comparator.comparing (person::getnamepinyin). thencomparing (Person::getage)). Collect (Collectors.tolist ());list<person> result =Persons.stream (). Sorted (comparator.comparing (person p)-P.getnamepinyin ()). Thencomparing (Person::getage)). Collect (Collectors.tolist ()); LongEnd =System.currenttimemillis (); System. out. println ("Duration Time:"+ (End-start) +"Ms"); for(person P:result) {System. out. println (P); } } Public StaticList<person>getpersons () {List<Person> persons =NewArraylist<>(); Persons.add (NewPerson ("China", -)); Persons.add (NewPerson ("China", at)); Persons.add (NewPerson ("China", +)); Persons.add (NewPerson ("United States", at)); Persons.add (NewPerson ("Thailand", at)); Persons.add (NewPerson ("South Korea", at)); Persons.add (NewPerson ("Japan", at)); returnpersons; }
Both of these methods are available.
The Comparator.comparing () method accepts a Function parameter, and you can use a lambda expression or a method reference.
Reference documents:
78359045
50748202
Multi-conditional ordering of list objects in Java 8