基本命名准则:
如果只处理对象而非基本类型,名称则为
Function
,Consumer
,Predicate
等。参数类型通过泛型添加。如果接收的参数是基本类型,则由名称的第一部分表示,如
LongConsumer
,DoubleFunction
,IntPredicate
等,但基本Supplier
类型例外。如果返回值为基本类型,则用
To
表示,如ToLongFunction <T>
和IntToLongFunction
。如果返回值类型与参数类型一致,则是一个运算符:单个参数使用
UnaryOperator
,两个参数使用BinaryOperator
。如果接收两个参数且返回值为布尔值,则是一个谓词(Predicate)。
如果接收的两个参数类型不同,则名称中有一个
Bi
。
name | type | description |
---|---|---|
Consumer | Consumer< T > | 接收T对象,不返回值 |
Predicate | Predicate< T > | 接收T对象并返回boolean |
Function | Function< T, R > | 接收T对象,返回R对象 |
Supplier | Supplier< T > | 返回T对象(例如工厂),不接收值 |
UnaryOperator | UnaryOperator | 接收T对象,返回T对象 |
BinaryOperator | BinaryOperator | 接收两个T对象,返回T对象 |
目标类型:
特征 | 函数式方法名 | 示例 |
---|---|---|
无参数; 无返回值 | Runnable (java.lang) run() | Runnable |
无参数; 返回类型任意 | Supplier get() getAs类型() | Supplier<T> BooleanSupplier IntSupplier LongSupplier DoubleSupplier |
无参数; 返回类型任意 | Callable (java.util.concurrent) call() | Callable<V> |
1 参数; 无返回值 | Consumer accept() | Consumer<T> IntConsumer LongConsumer DoubleConsumer |
2 参数 Consumer | BiConsumer accept() | BiConsumer<T,U> |
2 参数 Consumer; 1 引用; 1 基本类型 | Obj类型Consumer accept() | ObjIntConsumer<T> ObjLongConsumer<T> ObjDoubleConsumer<T> |
1 参数; 返回类型不同 | Function apply() To类型 和 类型To类型 applyAs类型() | Function<T,R> IntFunction <R> LongFunction<R> DoubleFunction <R> ToIntFunction <T> ToLongFunction<T> ToDoubleFunction<T> IntToLongFunction IntToDoubleFunction LongToIntFunction LongToDoubleFunction DoubleToIntFunction DoubleToLongFunction |
1 参数; 返回类型相同 | UnaryOperator apply() | UnaryOperator<T> IntUnaryOperator LongUnaryOperator DoubleUnaryOperator |
2 参数类型相同; 返回类型相同 | BinaryOperator apply() | BinaryOperator<T> IntBinaryOperator LongBinaryOperator DoubleBinaryOperator |
2 参数类型相同; 返回整型 | Comparator (java.util) compare() | Comparator<T> |
2 参数; 返回布尔型 | Predicate test() | Predicate<T> BiPredicate<T,U> IntPredicate LongPredicate DoublePredicate |
参数基本类型; 返回基本类型 | 类型To类型Function applyAs类型() | IntToLongFunction IntToDoubleFunction LongToIntFunction LongToDoubleFunction DoubleToIntFunction DoubleToLongFunction |
2 参数类型不同 | Bi操作 (不同方法名) | BiFunction<T,U,R> BiConsumer<T,U> BiPredicate<T,U> ToIntBiFunction<T,U> ToLongBiFunction<T,U> ToDoubleBiFunction<T> |
此表仅提供些常规方案。通过上表,你应该或多或少能自行推导出更多行的函数式接口。
java.util.function包有一些缺陷:没有 LongComparator
和 DoubleComparator
。有 BooleanSupplier
却没有其他表示 Boolean 的接口;有通用的 BiConsumer
却没有用于 int,long 和 double 的 BiConsumers
变体等。
示例:
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;
}
}
Function<Foo, Bar> f1 = f -> new Bar(f);
IntFunction<IBaz> f2 = i -> new IBaz(i);
LongFunction<LBaz> f3 = l -> new LBaz(l);
DoubleFunction<DBaz> f4 = d -> new DBaz(d);
ToIntFunction<IBaz> f5 = ib -> ib.i;
ToLongFunction<LBaz> f6 = lb -> lb.l;
ToDoubleFunction<DBaz> f7 = db -> db.d;
IntToLongFunction f8 = i -> i;
IntToDoubleFunction f9 = i -> i;
LongToIntFunction f10 = l -> (int)l;
LongToDoubleFunction f11 = l -> l;
DoubleToIntFunction f12 = d -> (int)d;
DoubleToLongFunction f13 = d -> (long)d;
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(13.0);
l = f13.applyAsLong(13.0);
在某些情况下,有必要进行强制类型转换,否则编译器会报截断错误。
主方法中的每个测试都显示了 Function 接口中不同类型的 apply() 方法。 每个都产生一个与其关联的 Lambda 表达式的调用。
方法引用
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();
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());
Function<AA,CC> f = ClassFunctionals::f5;
CC cc = f.apply(new AA());
BiFunction<AA,BB,CC> bif = ClassFunctionals::f6;
cc = bif.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());
}
}
方法签名(参数类型、返回类型)必须与接口的相同。