函数式接口总结

张开发
2026/4/15 8:24:18 15 分钟阅读

分享文章

函数式接口总结
函数式接口总结四大核心接口接口方法签名用途示例FunctionT, RR apply(T t)转换型输入 T 返回 RFunctionString, Integer f String::length;Consumervoid accept(T t)消费型只进不出ConsumerString c System.out::println;SupplierT get()供给型只出不进SupplierDouble s Math::random;Predicateboolean test(T t)断言型返回布尔值PredicateString p String::isEmpty;分类速查Function 系转换型有输入有输出接口输入输出特有方法FunctionT,RTRandThen,compose,identity()BiFunctionT,U,RT, URandThenUnaryOperatorTTT继承 Functionidentity()BinaryOperatorTT, TT继承 BiFunctionminBy,maxByIntFunctionRintRapplyLongFunctionRlongRapplyDoubleFunctionRdoubleRapplyToIntFunctionTTintapplyAsIntToLongFunctionTTlongapplyAsLongToDoubleFunctionTTdoubleapplyAsDoubleIntToLongFunctionintlongapplyAsLongIntToDoubleFunctionintdoubleapplyAsDoubleLongToIntFunctionlongintapplyAsIntLongToDoubleFunctionlongdoubleapplyAsDoubleDoubleToIntFunctiondoubleintapplyAsIntDoubleToLongFunctiondoublelongapplyAsLongToIntBiFunctionT,UT, UintapplyAsIntToLongBiFunctionT,UT, UlongapplyAsLongToDoubleBiFunctionT,UT, UdoubleapplyAsDoubleConsumer 系消费型有输入无输出接口输入特有方法ConsumerTTandThen,acceptBiConsumerT,UT, UandThen,acceptIntConsumerintandThen,acceptLongConsumerlongandThen,acceptDoubleConsumerdoubleandThen,acceptObjIntConsumerTT, intacceptObjLongConsumerTT, longacceptObjDoubleConsumerTT, doubleaccept###Supplier 系供给型无输入有输出接口输出示例SupplierTT() - new User()BooleanSupplierboolean() - trueIntSupplierint() - 42LongSupplierlongSystem::currentTimeMillisDoubleSupplierdoubleMath::randomPredicate 系断言型返回 boolean接口输入特有方法PredicateTTand,or,negate,isEqualBiPredicateT,UT, Uand,or,negateIntPredicateintand,or,negateLongPredicatelongand,or,negateDoublePredicatedoubleand,or,negate命名规律速记前缀/后缀含义示例Bi两个参数BiFunction,BiConsumer,BiPredicateUnary一元输入输出同类型UnaryOperatorT即FunctionT,TBinary二元输入输出同类型BinaryOperatorT即BiFunctionT,T,TToInt/ToLong/ToDouble返回基本类型ToIntFunctionTInt/Long/Double前缀参数是基本类型IntFunctionR,IntConsumerObj 基本类型对象 基本类型参数ObjIntConsumerT

更多文章