A summary of the parameters and return type of functional interfaces in the package java.util.function

陌路散爱 提交于 2019-12-05 01:43:08

问题


I'm looking for a table of parameters and return type of the single abstract method (SAM) of all interfaces in java.util.function.


回答1:


Here's a table of all 43 interfaces in the package, plus some other notable interfaces. This arrangement should make it easy to see the naming patterns in the package. The table is designed to work as a comment in a .java class file. Open the file in eclipse (or any other IDE that can resolve class names in comments). You should be able to hover over the names and see their javadocs. A ctrl-click will open the interfaces source code if you've properly attached the java source code.

(Surprisingly, this doesn't seem to work in InteliJ. Let me know if there's a setting I'm missing.)

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

Interfaces whose abstract method declares "throws Exception" denoted with *

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

Some examples of use:

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

Also, this book presents the package with some detailed tables.

And, while it's not much of a table, it's always good to read the official package summery.

The JDK actually has 57 Interfaces sporting the @FunctionalInterface annotation. Those not mentioned above include:

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

*/

However, the JDK has many interfaces that meet all the requirements to be a functional interface that don't have the @FunctionalInterface annotation (such as AutoClosable). The missing annotation doesn't keep them from working as a functional interface. It's used to force the compiler to throw an error when an interface violates the definition of a functional interface. In a way, it's a promise not to expand the set of abstract methods you must override when you implement the interface (it's ok to add default methods since they always have their own implementation). Which leaves me wondering: why it isn't @FunctionalInterface used on all the interfaces in the JDK that qualify?




回答2:


There are 43 interfaces in the package java.util.function in total. 35 of them are summarized in the "General" tables below (it is written in plaintext since StackOverflow does not support HTML tables):

General 1
---------

 ->         Return Type
|           R                   boolean            void
V           -                   -------            ----
   T        Function<T,R>       Predicate<T>       Consumer<T>
P  int      IntFunction<R>      IntPredicate       IntConsumer
a  long     LongFunction<R>     LongPredicate      LongConsumer
r  double   DoubleFunction<R>   DoublePredicate    DoubleConsumer
a  T,U      BiFunction<T,U,R>   BiPredicate<T,U>   BiConsumer<T,U>
m  void     Supplier<T>         BooleanSupplier    -

General 2
---------

 ->         Return Type
|           int                    long                    double
V           ---                    ----                    ------
   T        ToIntFunction<T>       ToLongFunction<T>       ToDoubleFunction<T>
P  int      IntUnaryOperator       IntToLongFunction       IntToDoubleFunction
a  long     LongToIntFunction      LongUnaryOperator       LongToDoubleFunction
r  double   DoubleToIntFunction    DoubleToLongFunction    DoubleUnaryOperator
a  T,U      ToIntBiFunction<T,U>   ToLongBiFunction<T,U>   ToDoubleBiFunction<T,U>
m  void     IntSupplier            LongSupplier            DoubleSupplier

The remaining 8 interfaces not included in the "General" tables above are: IntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator, ObjIntConsumer<T>, ObjLongConsumer<T>, ObjDoubleConsumer<T>, UnaryOperator<T>, BinaryOperator<T>. They are shown in the following tables. Related interfaces are also shown for the ease of comparison:

Operators
---------

 ->                Return Type
|                  R
V                  -
   T               Function<T,R>
                   UnaryOperator<T> = Function<T,T>
   T,U             BiFunction<T,U,R>
                   BinaryOperator<T> = BiFunction<T,T,T>
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<T>
   int        IntConsumer
   long       LongConsumer
P  double     DoubleConsumer
a  T,U        BiConsumer<T,U>
r  T,int      ObjIntConsumer<T>
a  T,long     ObjLongConsumer<T>
m  T,double   ObjDoubleConsumer<T>

Note

  • The type parameter of Supplier<T> is T in the original source code (T is the return type of the abstract method). However, it fits in the column R in this table since it is effectively the same.

  • As to complete the bottom-right corner entry of the "General 1" table above, java.lang.Runnable could be considered a void-void interface.

  • UnaryOperator<T> is an alias (sub-interface in Java terms) of Function<T,T>.

  • BinaryOperator<T> is an alias (sub-interface in Java terms) of BiFunction<T,T,T>

Naming Rules

  1. Interfaces with SAM (single abstract method) that takes void as the only parameter have a Consumer suffix in their name;

  2. Interfaces with SAM that returns void have a Supplier suffix in their name;

  3. Interfaces with SAM that returns boolean have a Predicate suffix in their name;

  4. Interfaces with SAM that takes one parameter and returns the same type have a UnaryOperator suffix in their name;

  5. Interfaces with SAM that takes two parameters of the same type and returns the same type have a BinaryOperator suffix in their name;

  6. All other interfaces have a Function suffix in their name;

  7. Interfaces with SAM that takes two parameters of different types have a Bi prefix before their suffices (as in BiConsumer, BiPredicate and BiFunction).

The above table in another format (since the above one may not display well in mobile devices):

 
 
P
a
r
a
m
 
 
T
int
long
double
T,U
void
Return Type
R
-----------------
Function<T,R>
IntFunction<R>
LongFunction<R>
DoubleFunction<R>
BiFunction<T,U,R>
Supplier<T>

 
 
P
a
r
a
m
 
 
T
int
long
double
T,U
void
Return Type
int
--------------------
ToIntFunction<T>
IntUnaryOperator
LongToIntFunction
DoubleToIntFunction
ToIntBiFunction<T,U>
IntSupplier

 
 
P
a
r
a
m
 
 
T
int
long
double
T,U
void
Return Type
long
---------------------
ToLongFunction<T>
IntToLongFunction
LongUnaryOperator
DoubleToLongFunction
ToLongBiFunction<T,U>
LongSupplier

 
 
P
a
r
a
m
 
 
T
int
long
double
T,U
void
Return Type
double
-----------------------
ToDoubleFunction<T>
IntToDoubleFunction
LongToDoubleFunction
DoubleUnaryOperator
ToDoubleBiFunction<T,U>
DoubleSupplier

 
 
P
a
r
a
m
 
 
T
int
long
double
T,U
void
Return Type
boolean
----------------
Predicate<T>
IntPredicate
LongPredicate
DoublePredicate
BiPredicate<T,U>
BooleanSupplier

 
 
P
a
r
a
m
 
 
T
int
long
double
T,U
void
Return Type
void
---------------
Consumer<T>
IntConsumer
LongConsumer
DoubleConsumer
BiConsumer<T,U>
-



来源:https://stackoverflow.com/questions/27743315/a-summary-of-the-parameters-and-return-type-of-functional-interfaces-in-the-pack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!