Is there a pre-Java-8 functional interface that replaces java.util.function.Consumer <T>?

While waiting for the transition to Java 8, I am trying to write my code in such a way that it facilitates the use of lambda.

I need a functional interface with a single method that takes a single argument of some type Tand returns void. This is the signature of the method , but of course I can’t use it yet.java.util.function.Consumer accept()

Is there any other interface in the standard Java 7 (and preferably Java 6) API that I can use instead? I know that I can create my own, but especially. until this code is ported to Java 8, it’s better for readability if I can use a standard interface that is already familiar with the standard Java 6/7 APIs.

The closest thing I have found so far, com.google.common.base.Function<T,Void>but (a) it is not part of the standard Java API and (b) its documentation says that "function instances, as a rule, should be transparent by reference - no side effects", which contradicts my intended use (with return type void).

+4
source share
2 answers

You can create your own interface:

public interface Invokable <T>{
    public void invoke(T param);
}

Alternatively, you can use the same interface as Java 8 Consumer. The source is included in the Java 8 JDK Early Access download, here with all comments and annotations removed:

public interface Consumer<T>{
    void accept(T t)
    default Consumer<T> andThen(Consumer<? super T> after)
}
+9
source
+2

Source: https://habr.com/ru/post/1529892/


All Articles