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; opt = opt.filter( s->s.equalsIgnoreCase("meh") ); System.out.println(opt+" <-- opt"); // Lambda using Consumer; opt.ifPresent( s->System.out.println(s) ); // Lambda using Function; opt = opt.map(s->s+"!").map(s->s+"!"); System.out.println(opt+" <-- opt"); // Lambda using Supplier; 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.functionjava.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个接口是: IntBinaryOperatorLongBinaryOperatorDoubleBinaryOperatorObjIntConsumerObjLongConsumerObjDoubleConsumerUnaryOperatorBinaryOperator 。 它们显示在下表中。 为便于比较,还显示了相关的接口:

 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的类型参数在原始源代码中是TT是抽象方法的返回类型)。 但是,它适用于该表中的R列,因为它实际上是相同的。

  • 至于完成上面“General 1”表的右下角条目, java.lang.Runnable可以被认为是一个voidvoid接口。

  • UnaryOperatorFunction的别名(Java术语中的子接口)。

  • BinaryOperatorBiFunction的别名(Java术语中的子接口)

命名规则

  1. 与SAM(单一抽象方法)的接口将void作为唯一参数在其名称中具有Consumer后缀;

  2. 返回void SAM接口在其名称中有一个Supplier后缀;

  3. 返回boolean SAM接口名称后缀为Predicate ;

  4. 与SAM接口,它接受一个参数并返回相同类型的名称UnaryOperator有一个UnaryOperator后缀;

  5. SAM与SAM的接口采用相同类型的两个参数并返回相同类型,其名称BinaryOperator有一个BinaryOperator后缀;

  6. 所有其他接口的名称都有一个Function后缀;

  7. 采用不同类型的两个参数的SAM接口在其足够之前具有Bi前缀(如BiConsumerBiPredicateBiFunction )。

上表另有格式(因为上面的一个可能无法在移动设备中很好地显示):

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
-