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

时间:2015-01-02 13:56:04

标签: java functional-programming functional-interface

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

1 个答案:

答案 0 :(得分:8)

这里是包中所有43个接口的表,以及一些其他值得注意的接口。这种安排应该可以很容易地看到包装中的命名模式。该表旨在用作.java类文件中的注释。在eclipse(或任何其他可以解析注释中的类名的IDE)中打开文件。您应该可以将鼠标悬停在名称上并查看其javadoc。如果您attached the java source code detailed tablesctrl-click会打开界面源代码。

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

import java.util.function.Function;  //Prevent "which package?" popups
import java.util.function.Predicate; 

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

表示
/*  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 */

一些使用示例:

// 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)

此外,本书还提供了一些包含official package summery的包。

而且,虽然它不是一张桌子,但阅读@FunctionalInterface annotation总是好的。

JDK实际上有57个接口运行why it isn't @FunctionalInterface used on all the interfaces in the JDK that qualify?。上面没有提到的那些包括:

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);    

*/

但是,JDK有许多接口可以满足作为不具有@FunctionalInterface注释的功能接口(例如AutoClosable)的所有要求。缺失的注释并不能阻止它们作为功能界面工作。当接口违反功能接口的定义时,它会强制编译器抛出错误。在某种程度上,它承诺不扩展您在实现接口时必须覆盖的抽象方法集(可以添加默认方法,因为它们总是有自己的实现)。这让我感到疑惑:{{3}}