java.util.function包中函数接口的参数和返回类型的摘要
我正在寻找一个参数表和java.util.function
中所有接口的单个抽象方法(SAM)的返回类型。
这是包中所有43个接口的表,以及一些其他值得注意的接口。 这种安排应该可以很容易地看到包装中的命名模式。 该表旨在用作.java类文件中的注释。 在eclipse(或任何其他可以解析注释中的类名称的IDE)中打开文件。 您应该可以将鼠标hover在名称上并查看其javadoc。 如果已正确附加java源代码,则ctrl-click
将打开interfaces 源代码 。
(令人惊讶的是,这在InteliJ中似乎不起作用。如果有一个我缺少的设置,请告诉我。)
import java.util.function.Function; //Prevent "which package?" popups import java.util.function.Predicate;
其抽象方法声明“抛出exception”的接口用*表示
/* Param\Return void boolean R ---- ------- - void Runnable BooleanSupplier Supplier void AutoCloseable* Callable * T Consumer Predicate Function R UnaryOperator T, U BiConsumer BiPredicate BiFunction R, R BinaryOperator int IntConsumer IntPredicate IntFunction T, int ObjIntConsumer long LongConsumer LongPredicate LongFunction T, long ObjLongConsumer double DoubleConsumer DoublePredicate DoubleFunction T, double ObjDoubleConsumer Param\Return int long double --- ---- ------ void IntSupplier LongSupplier DoubleSupplier T ToIntFunction ToLongFunction ToDoubleFunction T,U ToIntBiFunction ToLongBiFunction ToDoubleBiFunction int IntUnaryOperator IntToLongFunction IntToDoubleFunction int, int IntBinaryOperator long LongToIntFunction LongUnaryOperator LongToDoubleFunction long, long LongBinaryOperator double DoubleToIntFunction DoubleToLongFunction DoubleUnaryOperator double, double DoubleBinaryOperator */
一些使用示例:
// Lambda using Runnable new Thread(() -> System.out.println(Thread.currentThread().getName())).start(); Optional opt = Optional.of("Meh"); // Lambda using Predicate super String>; opt = opt.filter( s->s.equalsIgnoreCase("meh") ); System.out.println(opt+" <-- opt"); // Lambda using Consumer super String>; opt.ifPresent( s->System.out.println(s) ); // Lambda using Function super String, ? extends String>; opt = opt.map(s->s+"!").map(s->s+"!"); System.out.println(opt+" <-- opt"); // Lambda using Supplier extends IllegalArgumentException>; opt.orElseThrow( ()->new IllegalArgumentException("Should not be empty.") ); opt = Optional.empty(); opt.orElseThrow( ()->new IllegalArgumentException("Empty? Who said you could be empty?") ); Thread-0 Optional[Meh] <-- opt Meh Optional[Meh!!] <-- opt Exception in thread "main" java.lang.IllegalArgumentException: Empty? Who said you could be empty? at functionalinterfacestudy.AllLambdas.lambda$6(AllLambdas.java:110) at functionalinterfacestudy.AllLambdas$$Lambda$7/1392838282.get(Unknown Source) at java.util.Optional.orElseThrow(Unknown Source) at functionalinterfacestudy.AllLambdas.main(AllLambdas.java:110)
此外,本书还提供了一些详细的表格 。
而且,虽然它不是一张桌子,但阅读官方包装夏季总是好的。
JDK实际上有57个接口,带有@FunctionalInterface注释 。 上面没有提到的那些包括:
import java.io.FileFilter; // Aren't name collisions fun? import java.io.FilenameFilter; import java.util.Comparator; import java.util.logging.Filter; /* Interface Single Abstract Method --------- ---------------------- KeyEventDispatcher: boolean dispatchKeyEvent(KeyEvent e); KeyEventPostProcessor: boolean postProcessKeyEvent(KeyEvent e); FileFilter: boolean accept(File pathname); FilenameFilter: boolean accept(File dir, String name); Thread.UncaughtExceptionHandler: void uncaughtException(Thread t, Throwable e); DirectoryStream.Filter : boolean accept(T entry) throws IOException; PathMatcher: boolean matches(Path path); TemporalAdjuster: Temporal adjustInto(Temporal temporal); TemporalQuery: R queryFrom(TemporalAccessor temporal); Comparator: int compare(T o1, T o2); Filter: public boolean isLoggable(LogRecord record); PreferenceChangeListener: void preferenceChange(PreferenceChangeEvent evt); */
但是,JDK有许多接口可以满足作为没有@FunctionalInterface
注释的function接口的所有要求(例如AutoClosable
)。 缺少的注释不会阻止它们作为function接口工作。 当接口违反function接口的定义时,它用于强制编译器抛出错误。 在某种程度上,它承诺不扩展在实现接口时必须覆盖的抽象方法集(可以添加默认方法,因为它们总是有自己的实现)。 这让我感到疑惑: 为什么JDK中所有接口上使用的@FunctionalInterface
都不合格?
总共有java.util.function
包java.util.function
有43个接口。 其中35个在下面的“常规”表中进行了总结(由于StackOverflow不支持HTML表格,因此以纯文本编写):
General 1 --------- -> Return Type | R boolean void V - ------- ---- T Function Predicate Consumer P int IntFunction IntPredicate IntConsumer a long LongFunction LongPredicate LongConsumer r double DoubleFunction DoublePredicate DoubleConsumer a T,U BiFunction BiPredicate BiConsumer m void Supplier BooleanSupplier -
General 2 --------- -> Return Type | int long double V --- ---- ------ T ToIntFunction ToLongFunction ToDoubleFunction P int IntUnaryOperator IntToLongFunction IntToDoubleFunction a long LongToIntFunction LongUnaryOperator LongToDoubleFunction r double DoubleToIntFunction DoubleToLongFunction DoubleUnaryOperator a T,U ToIntBiFunction ToLongBiFunction ToDoubleBiFunction m void IntSupplier LongSupplier DoubleSupplier
上面“常规”表中未包含的其余8个接口是: IntBinaryOperator
, LongBinaryOperator
, DoubleBinaryOperator
, ObjIntConsumer
, ObjLongConsumer
, ObjDoubleConsumer
, UnaryOperator
, BinaryOperator
。 它们显示在下表中。 为便于比较,还显示了相关的接口:
Operators --------- -> Return Type | R V - T Function UnaryOperator = Function T,U BiFunction BinaryOperator = BiFunction P a int r --- a int IntUnaryOperator m int,int IntBinaryOperator e t long e ---- r long LongUnaryOperator s long,long LongBinaryOperator double ------ double DoubleUnaryOperator double,double DoubleBinaryOperator
Consumers --------- -> Return Type | void V ---- T Consumer int IntConsumer long LongConsumer P double DoubleConsumer a T,U BiConsumer r T,int ObjIntConsumer a T,long ObjLongConsumer m T,double ObjDoubleConsumer
注意
-
Supplier
的类型参数在原始源代码中是T
(T
是抽象方法的返回类型)。 但是,它适用于该表中的R
列,因为它实际上是相同的。 -
至于完成上面“General 1”表的右下角条目,
java.lang.Runnable
可以被认为是一个void
–void
接口。 -
UnaryOperator
是Function
的别名(Java术语中的子接口)。 -
BinaryOperator
是BiFunction
的别名(Java术语中的子接口)
命名规则
-
与SAM(单一抽象方法)的接口将
void
作为唯一参数在其名称中具有Consumer
后缀; -
返回
void
SAM接口在其名称中有一个Supplier
后缀; -
返回
boolean
SAM接口名称后缀为Predicate
; -
与SAM接口,它接受一个参数并返回相同类型的名称
UnaryOperator
有一个UnaryOperator
后缀; -
SAM与SAM的接口采用相同类型的两个参数并返回相同类型,其名称
BinaryOperator
有一个BinaryOperator
后缀; -
所有其他接口的名称都有一个
Function
后缀; -
采用不同类型的两个参数的SAM接口在其足够之前具有
Bi
前缀(如BiConsumer
,BiPredicate
和BiFunction
)。
上表另有格式(因为上面的一个可能无法在移动设备中很好地显示):
P
一个
[R
一个
米
Ť
INT
长
双
T,U
空虚
退货类型
[R
-----------------
function
IntFunction
LongFunction
DoubleFunction
双function
供应商
P
一个
[R
一个
米
Ť
INT
长
双
T,U
空虚
退货类型
INT
--------------------
ToIntFunction
IntUnaryOperator
LongToIntFunction
DoubleToIntFunction
ToIntBiFunction
IntSupplier
P
一个
[R
一个
米
Ť
INT
长
双
T,U
空虚
退货类型
长
---------------------
ToLongFunction
IntToLongFunction
LongUnaryOperator
DoubleToLongFunction
ToLongBiFunction
LongSupplier
P
一个
[R
一个
米
Ť
INT
长
双
T,U
空虚
退货类型
双
-----------------------
ToDoubleFunction
IntToDoubleFunction
LongToDoubleFunction
DoubleUnaryOperator
ToDoubleBiFunction
DoubleSupplier
P
一个
[R
一个
米
Ť
INT
长
双
T,U
空虚
退货类型
布尔
----------------
谓词
IntPredicate
LongPredicate
DoublePredicate
BiPredicate
BooleanSupplier
P
一个
[R
一个
米
Ť
INT
长
双
T,U
空虚
退货类型
空虚
---------------
消费者
IntConsumer
LongConsumer
DoubleConsumer
BiConsumer
-