函式式介面
此文章僅僅記錄函式式分類的學習
函式式介面主要用于接受,Lambda 運算式 和 方法參考 的賦值,定義其目標元素,每個函式式介面只包含一個抽象方法(函式式方法)
//函式式介面(只有一個方法)
interface Test{void aaa()}
/* 等同于
@FunctionalInterface
interface AAA{
void aaa();
}
*/
class TestImpl{
void aaa(){
System.out.println("方法參考");
}
}
public class TestLambda{
public static void main(String[]
\\Lambda運算式
Test t = () -> System.out.println("Lambda");
t.aaa();
\\方法參考
Test t2 = new TestImpl()::aaa;
t2.aaa();
}
}
可以看到,使用 Lambda 運算式 和 方法參考 時,需要定義一個介面(Test 介面),用于接受 Lambda 和 方法參考 的賦值,
為了避免每次都創建所需的介面,Java 8 引入了 java.util.function 包,其中包含了 一組介面 ,供不同情形使用,
類別庫中提供的介面



命名原則
- 如果只處理物件,而非基本型別,名稱則為:
Function,Consumer,Predicate等,引數型別通過泛型添加 - 如果接收的引數是基本型別,則由名稱的第一部分表示,如
LongConsumer,DoubleFunction,IntPredicate等,但回傳基本型別的Supplier介面例外, - 如果回傳值是基本型別,則用
To表示,如ToLongFunction<T>和IntToLongFunction - 如果回傳值型別與引數型別相同,則是一個
Operator:單個引數使用UnaryOperator, 兩個引數使用BinaryOperator, - 如果接收引數并回傳一個布林值,則是一個 謂詞(Predicate),
- 如果接受的兩個引數型別不同,則名稱中有一個
Bi
代碼實體
Lambda 運算式
class Foo{}
class Bar{
Foo f;
Bar(Foo f){
this.f = f;
}
}
class IBaz{
int i;
IBaz(int i){
this.i = i;
}
}
class LBaz{
long l;
LBaz(long l){
this.l = l;
}
}
class DBaz{
double d;
DBaz(double d){
this.d = d;
}
}
public class FunctionVariants {
static Function<Foo , Bar> f1 = f -> new Bar(f);
static IntFunction<IBaz> f2 = i -> new IBaz(i);
static LongFunction<LBaz> f3 = l -> new LBaz(l);
static DoubleFunction<DBaz> f4 = d -> new DBaz(d);
static ToIntFunction<IBaz> f5 = ib -> ib.i;
static ToLongFunction<LBaz> f6 = lb -> lb.l;
static ToDoubleFunction<DBaz> f7 = db -> db.d;
static IntToLongFunction f8 = i -> i;
static IntToDoubleFunction f9 = i -> i;
static LongToIntFunction f10 = l -> (int)l;
static LongToDoubleFunction f11 = l -> l;
static DoubleToIntFunction f12 = d -> (int)d;
static DoubleToLongFunction f13 = d -> (long)d;
public static void main(String[] args) {
Bar b = f1.apply(new Foo());
IBaz ib = f2.apply(11);
LBaz lb = f3.apply(11);
DBaz db = f4.apply(11);
int i = f5.applyAsInt(ib);
long l = f6.applyAsLong(lb);
double d = f7.applyAsDouble(db);
l = f8.applyAsLong(12);
d = f9.applyAsDouble(12);
i = f10.applyAsInt(12);
d = f11.applyAsDouble(12);
i = f12.applyAsInt(14.0);
l = f13.applyAsLong(14.0);
}
}
方法參考
class AA{}
class BB{}
class CC{}
public class ClassFunctionals {
static AA f1(){
return new AA();
}
static int f2(AA aa1 , AA aa2){
return 1;
}
static void f3(AA aa){
}
static void f4(AA aa , BB bb){
}
static CC f5(AA aa){
return new CC();
}
static CC f6(AA aa , BB bb){
return new CC();
}
static boolean f7(AA aa){
return true;
}
static boolean f8(AA aa , BB bb){
return true;
}
static AA f9(AA aa){
return new AA();
}
static AA f10(AA aa1 , AA aa2){
return new AA();
}
public static void main(String[] args) {
Supplier<AA> s = ClassFunctionals::f1;
s.get();
//2個引數,回傳型別為整形
Comparator<AA> c = ClassFunctionals::f2;
c.compare(new AA() , new AA());
Consumer<AA> cons = ClassFunctionals::f3;
cons.accept(new AA());
BiConsumer<AA , BB> bicons = ClassFunctionals::f4;
bicons.accept(new AA() , new BB());
//AA 代表引數型別,CC 代表回傳型別
Function<AA , CC> f = ClassFunctionals::f5;
CC cc = f.apply(new AA());
BiFunction<AA , BB ,CC> bic = ClassFunctionals::f6;
cc = bic.apply(new AA() , new BB());
Predicate<AA> p = ClassFunctionals::f7;
boolean result = p.test(new AA());
BiPredicate<AA , BB> bip = ClassFunctionals::f8;
result = bip.test(new AA() , new BB());
UnaryOperator<AA> uo = ClassFunctionals::f9;
AA aa = uo.apply(new AA());
BinaryOperator<AA> bo = ClassFunctionals::f10;
aa = bo.apply(new AA() , new AA());
}
}
參考 JAVA 編程思想
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/238884.html
標籤:Java
