I need an interface like:
interface Function<X,Y> {
Y eval(X obj);
}
Is there something like this in Java already or do I need to define my own?
An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class).
function Description. Functional interfaces provide target types for lambda expressions and method references. Each functional interface has a single abstract method, called the functional method for that functional interface, to which the lambda expression's parameter and return types are matched or adapted.
In Java. Java has no first-class functions, so function objects are usually expressed by an interface with a single method (most commonly the Callable interface), typically with the implementation being an anonymous inner class, or, starting in Java 8, a lambda.
The implements keyword is used to implement an interface . The interface keyword is used to declare a special type of class that only contains abstract methods. To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends ).
Check out Guava, it has a Function interface:
public interface Function<F, T> {
/**
* Applies the function to an object of type {@code F}, resulting in an object of type {@code T}.
* Note that types {@code F} and {@code T} may or may not be the same.
*
* @param from the source object
* @return the resulting object
*/
T apply(@Nullable F from);
/**
* Indicates whether some other object is equal to this {@code Function}. This method can return
* {@code true} <i>only</i> if the specified object is also a {@code Function} and, for every
* input object {@code o}, it returns exactly the same value. Thus, {@code
* function1.equals(function2)} implies that either {@code function1.apply(o)} and {@code
* function2.apply(o)} are both null, or {@code function1.apply(o).equals(function2.apply(o))}.
*
* <p>Note that it is always safe <i>not</i> to override {@link Object#equals}.
*/
boolean equals(@Nullable Object obj);
}
Unfortunately, there's no such thing in the core Java libraries. As a consequence, many libraries define their own function-like interface. If you happen to use such a library already, you can re-use the function it uses.
There is a built-in interface like this, although it's only compatible with Java 8 onwards. You can find it here.
From the Javadocs:
public interface Function<T,R>
Represents a function that accepts one argument and produces a result.
This is a functional interface whose functional method is
apply(Object)
.Type Parameters:
T
- the type of the input to the functionR
- the type of the result of the function
You can use a library such as Apache Commons Functor which has useful functions such as:
UnaryFunction
T evaluate(A obj);
BinaryFunction
T evaluate(L left, R right);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With