raisercostin raisercostin - 1 month ago 22
Java Question

Is there an interface similar to Callable but with arguments?

Is there an interface in Java similar to the

Callable
interface, that can accept an argument to its call method?

Like so:

public interface MyCallable<V> {
V call(String s) throws Exception;
}


I would rather avoid creating a new type if there already exists something that I can use. Or is there a better strategy to having multiple clients implement and plug in a callable routine?

Copied from here http://www.programmingforums.org/thread27905.html

Answer

Since Java 8 there is a whole set of Function-like interfaces in the java.util.function package. The one you're asking for specifically is simply Function.

Prior to Java 8, there was no general-purpose, built-in interface for this, but some libraries provided it.

For example Guava has the Function<F,T> interface with the method T apply(F input). It also makes heavy use of that interface in several places.