- All Known Subinterfaces:
- All Known Implementing Classes:
@GwtCompatible public interface Function<F extends @Nullable java.lang.Object,T extends @Nullable java.lang.Object>Determines an output value based on an input value; a pre-Java-8 version of
Functionsclass provides common functions and related utilities.
See the Guava User Guide article on the use of
For Java 8+ users
This interface is now a legacy type. Use
java.util.function.Function(or the appropriate primitive specialization such as
ToIntFunction) instead whenever possible. Otherwise, at least reduce explicit dependencies on this type by using lambda expressions or method references instead of classes, leaving your code easier to migrate in the future.
To use an existing function (say, named
function) in a context where the other type of function is expected, use the method reference
function::apply. A future version of
com.google.common.base.Functionwill be made to extend
java.util.function.Function, making conversion code necessary only in one direction. At that time, this interface will be officially discouraged.
- Kevin Bourrillion
applyReturns the result of applying this function to
input. This method is generally expected, but not absolutely required, to have the following properties:
- Its execution does not cause any observable side effects.
- The computation is consistent with equals; that is,
(a, b)implies that
inputis null and this function does not accept null arguments
boolean equals(@CheckForNull java.lang.Object object)May return
Functionthat behaves identically to this function.
Warning: do not depend on the behavior of this method.
Functioninstances in this library have implemented this method to recognize certain cases where distinct
Functioninstances would in fact behave identically. However, as code migrates to
java.util.function, that behavior will disappear. It is best not to depend on it.