Class Functions
com.google.common.base.Function instances; see that
 class for information about migrating to java.util.function.
 All methods return serializable functions as long as they're given serializable parameters.
See the Guava User Guide article on the use of Function.
- Since:
- 2.0
- Author:
- Mike Bostock, Jared Levy
- 
Method SummaryModifier and TypeMethodDescriptionstatic <A extends @Nullable Object, B extends @Nullable Object, C extends @Nullable Object>
 Function<A, C> Returns the composition of two functions.constant(E value) Returns a function that ignores its input and always returnsvalue.Returns a function which performs a map lookup with a default value.Returns a function which performs a map lookup.forPredicate(Predicate<T> predicate) Creates a function that returns the same boolean output as the given predicate for all inputs.forSupplier(Supplier<T> supplier) Returns a function that ignores its input and returns the result ofsupplier.get().identity()Returns the identity function.A function equivalent to the method referenceObject::toString.
- 
Method Details- 
toStringFunctionA function equivalent to the method referenceObject::toString. The function simply invokestoStringon its argument and returns the result. It throws aNullPointerExceptionon null input.Warning: The returned function may not be consistent with equals (as documented at Function.apply(F)). For example, this function yields different results for the two equal instancesImmutableSet.of(1, 2)andImmutableSet.of(2, 1).Warning: as with all function types in this package, avoid depending on the specific equals,hashCodeortoStringbehavior of the returned function. A future migration tojava.util.functionwill not preserve this behavior.As discussed above, prefer to use the method reference Object::toStringinstead, though note that it is not serializable unless you explicitly make itSerializable, typically by writing(Function<Object, String> & Serializable) Object::toString.For more important information about the transition from Guava's Functionclass to the JDKFunctionclass, seeFunction.
- 
identity
- 
forMappublic static <K extends @Nullable Object, V extends @Nullable Object> Function<K,V> forMap(Map<K, V> map) Returns a function which performs a map lookup. The returned function throws anIllegalArgumentExceptionif given a key that does not exist in the map. See alsoforMap(Map, Object), which returns a default value in this case.Note: if mapis aBiMap(or can be one), you can useMaps.asConverterinstead to get a function that also supports reverse conversion.If you are okay with nullbeing returned for an unrecognized key (instead of an exception being thrown), you can use the method referencemap::getinstead. Note that it is not serializable unless you explicitly make itSerializable, typically by writing(Function<K, V> & Serializable) map::get.
- 
forMappublic static <K extends @Nullable Object, V extends @Nullable Object> Function<K,V> forMap(Map<K, ? extends V> map, V defaultValue) Returns a function which performs a map lookup with a default value. The function created by this method returnsdefaultValuefor all inputs that do not belong to the map's key set. See alsoforMap(Map), which throws an exception in this case.Prefer to write the lambda expression k -> map.getOrDefault(k, defaultValue)instead. Note that it is not serializable unless you explicitly make itSerializable, typically by writing(Function<K, V> & Serializable) k -> map.getOrDefault(k, defaultValue).- Parameters:
- map- source map that determines the function behavior
- defaultValue- the value to return for inputs that aren't map keys
- Returns:
- function that returns map.get(a)whenais a key, ordefaultValueotherwise
 
- 
composepublic static <A extends @Nullable Object, B extends @Nullable Object, C extends @Nullable Object> Function<A,C> compose(Function<B, C> g, Function<A, ? extends B> f) Returns the composition of two functions. Forf: A->Bandg: B->C, composition is defined as the function h such thath(a) == g(f(a))for eacha.JRE users and Android users who opt in to library desugaring: use g.compose(f)or (probably clearer)f.andThen(g)instead. Note that it is not serializable.- Parameters:
- g- the second function to apply
- f- the first function to apply
- Returns:
- the composition of fandg
- See Also:
 
- 
forPredicateCreates a function that returns the same boolean output as the given predicate for all inputs.The returned function is consistent with equals (as documented at Function.apply(F)) if and only ifpredicateis itself consistent with equals.Prefer to use the method reference predicate::testinstead. Note that it is not serializable unless you explicitly make itSerializable, typically by writing(Function<T, Boolean> & Serializable) predicate::test.
- 
constantReturns a function that ignores its input and always returnsvalue.Prefer to use the lambda expression o -> valueinstead. Note that it is not serializable unless you explicitly make itSerializable, typically by writing(Function<Object, E> & Serializable) o -> value.- Parameters:
- value- the constant value for the function to return
- Returns:
- a function that always returns value
 
- 
forSupplierpublic static <F extends @Nullable Object, T extends @Nullable Object> Function<F,T> forSupplier(Supplier<T> supplier) Returns a function that ignores its input and returns the result ofsupplier.get().Prefer to use the lambda expression o -> supplier.get()instead. Note that it is not serializable unless you explicitly make itSerializable, typically by writing(Function<F, T> & Serializable) o -> supplier.get().- Since:
- 10.0
 
 
-