java.util.function接口的命名规则

java.util.function 接口的命名规则

  1. 如果接口只处理对象,而不是基本类型,那就会用一个直截了当的名字,像 Function、Consumer和Predicate等。参数类型通过泛型添加。
  2. 若接口接受一个基本类型的参数,则会用名字的第一部分来表示,例如LongConsumer、DoubleFunction和Predicate等。参数类型会通过泛型添加。
  3. 若接口返回的是基本类型的结果,则会用 To 来表示,例如 ToLongFunction<T> 和 IntToLongFunction。
  4. 若接口返回的类型和参数类型相同,则会被命名为Operator。UnaryOperator用于表示一个参数,BinaryOperator用于表示两个参数。
  5. 若接口接受一个参数并返回 boolean,则会被命名为Predicate。
  6. 若接口接受两个不同类型的参数,则名字中会有一个Bi(比如BiPredicate)。

java.util.function的目标类型

特点函数式方法命名 *用法
没有参数;没有返回值Runnable (java.lang) run()Runnable
没有参数;可以返回任何类型Supplier get() getAstype()Supplier<T> BooleanSupplier IntSupplier LongSupplier DoubleSupplier
没有参数;可以返回任何类型Callable (java.util.concurrent) call()Callable<V>
一个参数;没有返回值Consumer accept()Consumer<T> IntConsumer LongConsumer DoubleConsumer
两个参数的ConsumerBiConsumer accept()BiConsumer<T,U>
两个参数的Consumer;第一个参数是引用,第二个参数是基本类型ObjtypeConsumer accept()ObjIntConsumer<T> ObjLongConsumer<T> ObjDoubleConsumer<T>
一个参数;返回值为不同类型Function apply() Totype & typeTotype: applyAstype()Function<T,R> IntFunction<R> LongFunction<R> DoubleFunction<R> ToIntFunction<T> ToLongFunction<T> ToDoubleFunction<T> IntToLongFunction IntToDoubleFunction LongToIntFunction LongToDoubleFunction DoubleToIntFunction DoubleToLongFunction
一个参数;返回值为相同类型UnaryOperator apply()UnaryOperator<T> IntUnaryOperator LongUnaryOperator DoubleUnaryOperator
两个相同类型的参数;返回值也是相同类型BinaryOperator apply()BinaryOperator<T> IntBinaryOperator LongBinaryOperator DoubleBinaryOperator
两个相同类型的参数;返回intComparator (java.util) compare()Comparator<T>
两个参数;返回booleanPredicate test()Predicate<T> BiPredicate<T,U> IntPredicate LongPredicate DoublePredicate
基本类型的参数;返回值也是基本类型typeTotypeFunction applyAstype()IntToLongFunction IntToDoubleFunction LongToIntFunction LongToDoubleFunction DoubleToIntFunction DoubleToLongFunction
两个参数;不同类型Bi+操作名(方法名会变化)BiFunction<T,U,R> BiConsumer<T,U> BiPredicate<T,U> ToIntBiFunction<T,U> ToLongBiFunction<T,U> ToDoubleBiFunction<T,U>

摘自OnJava中文版13.4 函数式接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

残魁斜罡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值