Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: general function X->Y interface

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?

like image 348
Albert Avatar asked Oct 11 '10 15:10

Albert


People also ask

What is the function of interface?

An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class).

What functional interface is defined in the Java Util function package?

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.

How do you define a function object in Java?

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.

How do you implement a method in Java?

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 ).


4 Answers

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);
 }
like image 92
Nathan Hughes Avatar answered Sep 19 '22 19:09

Nathan Hughes


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.

like image 22
Jorn Avatar answered Sep 21 '22 19:09

Jorn


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 function
R - the type of the result of the function

like image 29
MinteZ Avatar answered Sep 19 '22 19:09

MinteZ


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); 
like image 20
dogbane Avatar answered Sep 18 '22 19:09

dogbane