Java Runnable, Callable, Supplier, etc.

Java doesn’t have the concept of delegate; instead, if you need a pointer to a function, you can create inline anonymous classes (or lambda expressions as of Java 8 ) which are implementations of certain interfaces designed for this propose (a.k.a functional interfaces as of Java 8). However as Java evolves, more of such interfaces are being added. While they may seem very similar and confusing, each of them has a unique characteristic which sets it apart from the others. You can map many of them to identical types in .NET. The following table lists some of the famous interfaces but there are many more. For example to support functions with two arguments, Java has another interface called BiFunction and if you need more arguments, you have to create your own interfaces. Remember that Java (up to version 10) doesn’t support identical class names if the only difference is number of type arguments. (In .NET there are various Func and Action types with up to 16 type arguments.)

Java Supports Exceptions Argument Returns .NET Equivalent
Callable<R>  Yes  No  Yes  Func<R>
Supplier<R>  No  No  Yes  Func<R>
Provider<T>  No  Yes  Yes  Func<T,T>
Function<T,R>  No  Yes  Yes  Func<T,R>
Consumer<T>  No  Yes  No  Action<T>
Runnable  No  No  No  Action

 

Advertisements