java.util.function包中功能接口的参数和返回类型的摘要

Siu*_*ji- 4 java functional-programming functional-interface

我正在寻找一个参数表和所有接口的单一抽象方法(SAM)的返回类型java.util.function.

can*_*nge 8

这是包中所有43个接口的表,以及一些其他值得注意的接口.这种安排应该可以很容易地看到包装中的命名模式.该表旨在用作.java类文件中的注释.在eclipse(或任何其他可以解析注释中的类名的IDE)中打开文件.您应该可以将鼠标悬停在名称上并查看其javadoc.ctrl-click如果您正确附加了Java源代码, A 将打开interfaces 源代码.

(令人惊讶的是,这似乎在InteliJ中没有用.如果有一个我缺少的设置,请告诉我.)

import java.util.function.Function;  //Prevent "which package?" popups
import java.util.function.Predicate; 
Run Code Online (Sandbox Code Playgroud)

其抽象方法声明"抛出异常"的接口用*表示

/*  Param\Return   void                  boolean                R                  
                   ----                  -------                -                  
void               Runnable              BooleanSupplier        Supplier<R>        
void               AutoCloseable*                               Callable<R>*        

T                  Consumer<T>           Predicate<T>           Function<T,R>      
R                                                               UnaryOperator<R>   

T, U               BiConsumer<T,U>       BiPredicate<T,U>       BiFunction<T,U,R>  
R, R                                                            BinaryOperator<R>  

int                IntConsumer           IntPredicate           IntFunction<R>     
T, int             ObjIntConsumer<T>                            

long               LongConsumer          LongPredicate          LongFunction<R>    
T, long            ObjLongConsumer<T>                           

double             DoubleConsumer        DoublePredicate        DoubleFunction<R>  
T, double          ObjDoubleConsumer<T>

    Param\Return   int                   long                   double
                   ---                   ----                   ------
void               IntSupplier           LongSupplier           DoubleSupplier

T                  ToIntFunction<T>      ToLongFunction<T>      ToDoubleFunction<T>

T,U                ToIntBiFunction<T,U>  ToLongBiFunction<T,U>  ToDoubleBiFunction<T,U>

int                IntUnaryOperator      IntToLongFunction      IntToDoubleFunction
int, int           IntBinaryOperator                                  

long               LongToIntFunction     LongUnaryOperator      LongToDoubleFunction
long, long                               LongBinaryOperator      

double             DoubleToIntFunction   DoubleToLongFunction   DoubleUnaryOperator
double, double                                                  DoubleBinaryOperator */
Run Code Online (Sandbox Code Playgroud)

一些使用示例:

// Lambda using Runnable
new Thread(() -> System.out.println(Thread.currentThread().getName())).start();

Optional<String> 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)
Run Code Online (Sandbox Code Playgroud)

此外,本书还提供了一些详细的表格.

而且,虽然它不是一张桌子,但阅读官方包装夏季总是好的.

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<T>.Filter<T>:    boolean accept(T entry) throws IOException;
PathMatcher:                     boolean matches(Path path);
TemporalAdjuster:                Temporal adjustInto(Temporal temporal);
TemporalQuery<R>:                R queryFrom(TemporalAccessor temporal);
Comparator<T>:                   int compare(T o1, T o2);
Filter:                          public boolean isLoggable(LogRecord record);
PreferenceChangeListener:        void preferenceChange(PreferenceChangeEvent evt);    

*/
Run Code Online (Sandbox Code Playgroud)

但是,JDK有许多接口可以满足@FunctionalInterface作为没有注释的功能接口的所有要求(例如AutoClosable).缺少的注释不会阻止它们作为功能接口工作.当接口违反功能接口的定义时,它用于强制编译器抛出错误.在某种程度上,它承诺不扩展在实现接口时必须覆盖的抽象方法集(可以添加默认方法,因为它们总是有自己的实现).这让我想知道:为什么@FunctionalInterfaceJDK中的所有接口都没有使用它?