Skip to content

Javafx like Callback but without return

I’m looking for a standard Javafx or java interface (if it exists) that acts like a Callback, except that it does not return a value.

The standard Callback from javafx.util package is as follows:

public interface Callback<P,R> {
    public R call(P param);
}

This is useful when you need to return the value, but I don’t. I’ve looked into Callable<T>:

public interface Callable<V> {
    V call() throws Exception;
}

But this doesn’t actually pass in a value into call. What I’m looking for is basically this:

public interface Callable<V> {
    void call(V value) throws Exception;
}

Is there a standard Java interface, or should I just create my own?

Answer

What are you looking for is Consumer. That’s added since java 8.

Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.

@FunctionalInterface
public interface Consumer<T> {

/**
 * Performs this operation on the given argument.
 *
 * @param t the input argument
 */
void accept(T t);