Interface Function<K,​V>

All Superinterfaces:
java.util.function.Function<K,​V>
All Known Subinterfaces:
Byte2ByteFunction, Byte2CharFunction, Byte2DoubleFunction, Byte2FloatFunction, Byte2IntFunction, Byte2LongFunction, Byte2ObjectFunction<V>, Byte2ReferenceFunction<V>, Byte2ShortFunction, Char2ByteFunction, Char2CharFunction, Char2DoubleFunction, Char2FloatFunction, Char2IntFunction, Char2LongFunction, Char2ObjectFunction<V>, Char2ReferenceFunction<V>, Char2ShortFunction, Double2ByteFunction, Double2CharFunction, Double2DoubleFunction, Double2DoubleMap, Double2DoubleSortedMap, Double2FloatFunction, Double2IntFunction, Double2IntMap, Double2IntSortedMap, Double2LongFunction, Double2LongMap, Double2LongSortedMap, Double2ObjectFunction<V>, Double2ObjectMap<V>, Double2ObjectSortedMap<V>, Double2ReferenceFunction<V>, Double2ShortFunction, Float2ByteFunction, Float2CharFunction, Float2DoubleFunction, Float2FloatFunction, Float2IntFunction, Float2LongFunction, Float2ObjectFunction<V>, Float2ReferenceFunction<V>, Float2ShortFunction, Int2ByteFunction, Int2CharFunction, Int2DoubleFunction, Int2DoubleMap, Int2DoubleSortedMap, Int2FloatFunction, Int2IntFunction, Int2IntMap, Int2IntSortedMap, Int2LongFunction, Int2LongMap, Int2LongSortedMap, Int2ObjectFunction<V>, Int2ObjectMap<V>, Int2ObjectSortedMap<V>, Int2ReferenceFunction<V>, Int2ShortFunction, Long2ByteFunction, Long2CharFunction, Long2DoubleFunction, Long2DoubleMap, Long2DoubleSortedMap, Long2FloatFunction, Long2IntFunction, Long2IntMap, Long2IntSortedMap, Long2LongFunction, Long2LongMap, Long2LongSortedMap, Long2ObjectFunction<V>, Long2ObjectMap<V>, Long2ObjectSortedMap<V>, Long2ReferenceFunction<V>, Long2ShortFunction, Object2ByteFunction<K>, Object2CharFunction<K>, Object2DoubleFunction<K>, Object2DoubleMap<K>, Object2DoubleSortedMap<K>, Object2FloatFunction<K>, Object2IntFunction<K>, Object2IntMap<K>, Object2IntSortedMap<K>, Object2LongFunction<K>, Object2LongMap<K>, Object2LongSortedMap<K>, Object2ObjectFunction<K,​V>, Object2ObjectMap<K,​V>, Object2ObjectSortedMap<K,​V>, Object2ReferenceFunction<K,​V>, Object2ShortFunction<K>, Reference2ByteFunction<K>, Reference2CharFunction<K>, Reference2DoubleFunction<K>, Reference2FloatFunction<K>, Reference2IntFunction<K>, Reference2LongFunction<K>, Reference2ObjectFunction<K,​V>, Reference2ReferenceFunction<K,​V>, Reference2ShortFunction<K>, Short2ByteFunction, Short2CharFunction, Short2DoubleFunction, Short2FloatFunction, Short2IntFunction, Short2LongFunction, Short2ObjectFunction<V>, Short2ReferenceFunction<V>, Short2ShortFunction
All Known Implementing Classes:
AbstractDouble2DoubleFunction, AbstractDouble2DoubleMap, AbstractDouble2DoubleSortedMap, AbstractDouble2IntFunction, AbstractDouble2IntMap, AbstractDouble2IntSortedMap, AbstractDouble2LongFunction, AbstractDouble2LongMap, AbstractDouble2LongSortedMap, AbstractDouble2ObjectFunction, AbstractDouble2ObjectMap, AbstractDouble2ObjectSortedMap, AbstractInt2DoubleFunction, AbstractInt2DoubleMap, AbstractInt2DoubleSortedMap, AbstractInt2IntFunction, AbstractInt2IntMap, AbstractInt2IntSortedMap, AbstractInt2LongFunction, AbstractInt2LongMap, AbstractInt2LongSortedMap, AbstractInt2ObjectFunction, AbstractInt2ObjectMap, AbstractInt2ObjectSortedMap, AbstractLong2DoubleFunction, AbstractLong2DoubleMap, AbstractLong2DoubleSortedMap, AbstractLong2IntFunction, AbstractLong2IntMap, AbstractLong2IntSortedMap, AbstractLong2LongFunction, AbstractLong2LongMap, AbstractLong2LongSortedMap, AbstractLong2ObjectFunction, AbstractLong2ObjectMap, AbstractLong2ObjectSortedMap, AbstractObject2DoubleFunction, AbstractObject2DoubleMap, AbstractObject2DoubleSortedMap, AbstractObject2IntFunction, AbstractObject2IntMap, AbstractObject2IntSortedMap, AbstractObject2LongFunction, AbstractObject2LongMap, AbstractObject2LongSortedMap, AbstractObject2ObjectFunction, AbstractObject2ObjectMap, AbstractObject2ObjectSortedMap, Double2DoubleArrayMap, Double2DoubleAVLTreeMap, Double2DoubleFunctions.EmptyFunction, Double2DoubleFunctions.PrimitiveFunction, Double2DoubleFunctions.Singleton, Double2DoubleFunctions.SynchronizedFunction, Double2DoubleFunctions.UnmodifiableFunction, Double2DoubleLinkedOpenHashMap, Double2DoubleMaps.EmptyMap, Double2DoubleMaps.Singleton, Double2DoubleMaps.SynchronizedMap, Double2DoubleMaps.UnmodifiableMap, Double2DoubleOpenCustomHashMap, Double2DoubleOpenHashMap, Double2DoubleRBTreeMap, Double2DoubleSortedMaps.EmptySortedMap, Double2DoubleSortedMaps.Singleton, Double2DoubleSortedMaps.SynchronizedSortedMap, Double2DoubleSortedMaps.UnmodifiableSortedMap, Double2IntArrayMap, Double2IntAVLTreeMap, Double2IntFunctions.EmptyFunction, Double2IntFunctions.PrimitiveFunction, Double2IntFunctions.Singleton, Double2IntFunctions.SynchronizedFunction, Double2IntFunctions.UnmodifiableFunction, Double2IntLinkedOpenHashMap, Double2IntMaps.EmptyMap, Double2IntMaps.Singleton, Double2IntMaps.SynchronizedMap, Double2IntMaps.UnmodifiableMap, Double2IntOpenCustomHashMap, Double2IntOpenHashMap, Double2IntRBTreeMap, Double2IntSortedMaps.EmptySortedMap, Double2IntSortedMaps.Singleton, Double2IntSortedMaps.SynchronizedSortedMap, Double2IntSortedMaps.UnmodifiableSortedMap, Double2LongArrayMap, Double2LongAVLTreeMap, Double2LongFunctions.EmptyFunction, Double2LongFunctions.PrimitiveFunction, Double2LongFunctions.Singleton, Double2LongFunctions.SynchronizedFunction, Double2LongFunctions.UnmodifiableFunction, Double2LongLinkedOpenHashMap, Double2LongMaps.EmptyMap, Double2LongMaps.Singleton, Double2LongMaps.SynchronizedMap, Double2LongMaps.UnmodifiableMap, Double2LongOpenCustomHashMap, Double2LongOpenHashMap, Double2LongRBTreeMap, Double2LongSortedMaps.EmptySortedMap, Double2LongSortedMaps.Singleton, Double2LongSortedMaps.SynchronizedSortedMap, Double2LongSortedMaps.UnmodifiableSortedMap, Double2ObjectArrayMap, Double2ObjectAVLTreeMap, Double2ObjectFunctions.EmptyFunction, Double2ObjectFunctions.PrimitiveFunction, Double2ObjectFunctions.Singleton, Double2ObjectFunctions.SynchronizedFunction, Double2ObjectFunctions.UnmodifiableFunction, Double2ObjectLinkedOpenHashMap, Double2ObjectMaps.EmptyMap, Double2ObjectMaps.Singleton, Double2ObjectMaps.SynchronizedMap, Double2ObjectMaps.UnmodifiableMap, Double2ObjectOpenCustomHashMap, Double2ObjectOpenHashMap, Double2ObjectRBTreeMap, Double2ObjectSortedMaps.EmptySortedMap, Double2ObjectSortedMaps.Singleton, Double2ObjectSortedMaps.SynchronizedSortedMap, Double2ObjectSortedMaps.UnmodifiableSortedMap, Int2DoubleArrayMap, Int2DoubleAVLTreeMap, Int2DoubleFunctions.EmptyFunction, Int2DoubleFunctions.PrimitiveFunction, Int2DoubleFunctions.Singleton, Int2DoubleFunctions.SynchronizedFunction, Int2DoubleFunctions.UnmodifiableFunction, Int2DoubleLinkedOpenHashMap, Int2DoubleMaps.EmptyMap, Int2DoubleMaps.Singleton, Int2DoubleMaps.SynchronizedMap, Int2DoubleMaps.UnmodifiableMap, Int2DoubleOpenCustomHashMap, Int2DoubleOpenHashMap, Int2DoubleRBTreeMap, Int2DoubleSortedMaps.EmptySortedMap, Int2DoubleSortedMaps.Singleton, Int2DoubleSortedMaps.SynchronizedSortedMap, Int2DoubleSortedMaps.UnmodifiableSortedMap, Int2IntArrayMap, Int2IntAVLTreeMap, Int2IntFunctions.EmptyFunction, Int2IntFunctions.PrimitiveFunction, Int2IntFunctions.Singleton, Int2IntFunctions.SynchronizedFunction, Int2IntFunctions.UnmodifiableFunction, Int2IntLinkedOpenHashMap, Int2IntMaps.EmptyMap, Int2IntMaps.Singleton, Int2IntMaps.SynchronizedMap, Int2IntMaps.UnmodifiableMap, Int2IntOpenCustomHashMap, Int2IntOpenHashMap, Int2IntRBTreeMap, Int2IntSortedMaps.EmptySortedMap, Int2IntSortedMaps.Singleton, Int2IntSortedMaps.SynchronizedSortedMap, Int2IntSortedMaps.UnmodifiableSortedMap, Int2LongArrayMap, Int2LongAVLTreeMap, Int2LongFunctions.EmptyFunction, Int2LongFunctions.PrimitiveFunction, Int2LongFunctions.Singleton, Int2LongFunctions.SynchronizedFunction, Int2LongFunctions.UnmodifiableFunction, Int2LongLinkedOpenHashMap, Int2LongMaps.EmptyMap, Int2LongMaps.Singleton, Int2LongMaps.SynchronizedMap, Int2LongMaps.UnmodifiableMap, Int2LongOpenCustomHashMap, Int2LongOpenHashMap, Int2LongRBTreeMap, Int2LongSortedMaps.EmptySortedMap, Int2LongSortedMaps.Singleton, Int2LongSortedMaps.SynchronizedSortedMap, Int2LongSortedMaps.UnmodifiableSortedMap, Int2ObjectArrayMap, Int2ObjectAVLTreeMap, Int2ObjectFunctions.EmptyFunction, Int2ObjectFunctions.PrimitiveFunction, Int2ObjectFunctions.Singleton, Int2ObjectFunctions.SynchronizedFunction, Int2ObjectFunctions.UnmodifiableFunction, Int2ObjectLinkedOpenHashMap, Int2ObjectMaps.EmptyMap, Int2ObjectMaps.Singleton, Int2ObjectMaps.SynchronizedMap, Int2ObjectMaps.UnmodifiableMap, Int2ObjectOpenCustomHashMap, Int2ObjectOpenHashMap, Int2ObjectRBTreeMap, Int2ObjectSortedMaps.EmptySortedMap, Int2ObjectSortedMaps.Singleton, Int2ObjectSortedMaps.SynchronizedSortedMap, Int2ObjectSortedMaps.UnmodifiableSortedMap, Long2DoubleArrayMap, Long2DoubleAVLTreeMap, Long2DoubleFunctions.EmptyFunction, Long2DoubleFunctions.PrimitiveFunction, Long2DoubleFunctions.Singleton, Long2DoubleFunctions.SynchronizedFunction, Long2DoubleFunctions.UnmodifiableFunction, Long2DoubleLinkedOpenHashMap, Long2DoubleMaps.EmptyMap, Long2DoubleMaps.Singleton, Long2DoubleMaps.SynchronizedMap, Long2DoubleMaps.UnmodifiableMap, Long2DoubleOpenCustomHashMap, Long2DoubleOpenHashMap, Long2DoubleRBTreeMap, Long2DoubleSortedMaps.EmptySortedMap, Long2DoubleSortedMaps.Singleton, Long2DoubleSortedMaps.SynchronizedSortedMap, Long2DoubleSortedMaps.UnmodifiableSortedMap, Long2IntArrayMap, Long2IntAVLTreeMap, Long2IntFunctions.EmptyFunction, Long2IntFunctions.PrimitiveFunction, Long2IntFunctions.Singleton, Long2IntFunctions.SynchronizedFunction, Long2IntFunctions.UnmodifiableFunction, Long2IntLinkedOpenHashMap, Long2IntMaps.EmptyMap, Long2IntMaps.Singleton, Long2IntMaps.SynchronizedMap, Long2IntMaps.UnmodifiableMap, Long2IntOpenCustomHashMap, Long2IntOpenHashMap, Long2IntRBTreeMap, Long2IntSortedMaps.EmptySortedMap, Long2IntSortedMaps.Singleton, Long2IntSortedMaps.SynchronizedSortedMap, Long2IntSortedMaps.UnmodifiableSortedMap, Long2LongArrayMap, Long2LongAVLTreeMap, Long2LongFunctions.EmptyFunction, Long2LongFunctions.PrimitiveFunction, Long2LongFunctions.Singleton, Long2LongFunctions.SynchronizedFunction, Long2LongFunctions.UnmodifiableFunction, Long2LongLinkedOpenHashMap, Long2LongMaps.EmptyMap, Long2LongMaps.Singleton, Long2LongMaps.SynchronizedMap, Long2LongMaps.UnmodifiableMap, Long2LongOpenCustomHashMap, Long2LongOpenHashMap, Long2LongRBTreeMap, Long2LongSortedMaps.EmptySortedMap, Long2LongSortedMaps.Singleton, Long2LongSortedMaps.SynchronizedSortedMap, Long2LongSortedMaps.UnmodifiableSortedMap, Long2ObjectArrayMap, Long2ObjectAVLTreeMap, Long2ObjectFunctions.EmptyFunction, Long2ObjectFunctions.PrimitiveFunction, Long2ObjectFunctions.Singleton, Long2ObjectFunctions.SynchronizedFunction, Long2ObjectFunctions.UnmodifiableFunction, Long2ObjectLinkedOpenHashMap, Long2ObjectMaps.EmptyMap, Long2ObjectMaps.Singleton, Long2ObjectMaps.SynchronizedMap, Long2ObjectMaps.UnmodifiableMap, Long2ObjectOpenCustomHashMap, Long2ObjectOpenHashMap, Long2ObjectRBTreeMap, Long2ObjectSortedMaps.EmptySortedMap, Long2ObjectSortedMaps.Singleton, Long2ObjectSortedMaps.SynchronizedSortedMap, Long2ObjectSortedMaps.UnmodifiableSortedMap, Object2DoubleArrayMap, Object2DoubleAVLTreeMap, Object2DoubleFunctions.EmptyFunction, Object2DoubleFunctions.PrimitiveFunction, Object2DoubleFunctions.Singleton, Object2DoubleFunctions.SynchronizedFunction, Object2DoubleFunctions.UnmodifiableFunction, Object2DoubleLinkedOpenCustomHashMap, Object2DoubleLinkedOpenHashMap, Object2DoubleMaps.EmptyMap, Object2DoubleMaps.Singleton, Object2DoubleMaps.SynchronizedMap, Object2DoubleMaps.UnmodifiableMap, Object2DoubleOpenCustomHashMap, Object2DoubleOpenHashMap, Object2DoubleRBTreeMap, Object2DoubleSortedMaps.EmptySortedMap, Object2DoubleSortedMaps.Singleton, Object2DoubleSortedMaps.SynchronizedSortedMap, Object2DoubleSortedMaps.UnmodifiableSortedMap, Object2IntArrayMap, Object2IntAVLTreeMap, Object2IntFunctions.EmptyFunction, Object2IntFunctions.PrimitiveFunction, Object2IntFunctions.Singleton, Object2IntFunctions.SynchronizedFunction, Object2IntFunctions.UnmodifiableFunction, Object2IntLinkedOpenCustomHashMap, Object2IntLinkedOpenHashMap, Object2IntMaps.EmptyMap, Object2IntMaps.Singleton, Object2IntMaps.SynchronizedMap, Object2IntMaps.UnmodifiableMap, Object2IntOpenCustomHashMap, Object2IntOpenHashMap, Object2IntRBTreeMap, Object2IntSortedMaps.EmptySortedMap, Object2IntSortedMaps.Singleton, Object2IntSortedMaps.SynchronizedSortedMap, Object2IntSortedMaps.UnmodifiableSortedMap, Object2LongArrayMap, Object2LongAVLTreeMap, Object2LongFunctions.EmptyFunction, Object2LongFunctions.PrimitiveFunction, Object2LongFunctions.Singleton, Object2LongFunctions.SynchronizedFunction, Object2LongFunctions.UnmodifiableFunction, Object2LongLinkedOpenCustomHashMap, Object2LongLinkedOpenHashMap, Object2LongMaps.EmptyMap, Object2LongMaps.Singleton, Object2LongMaps.SynchronizedMap, Object2LongMaps.UnmodifiableMap, Object2LongOpenCustomHashMap, Object2LongOpenHashMap, Object2LongRBTreeMap, Object2LongSortedMaps.EmptySortedMap, Object2LongSortedMaps.Singleton, Object2LongSortedMaps.SynchronizedSortedMap, Object2LongSortedMaps.UnmodifiableSortedMap, Object2ObjectArrayMap, Object2ObjectAVLTreeMap, Object2ObjectFunctions.EmptyFunction, Object2ObjectFunctions.Singleton, Object2ObjectFunctions.SynchronizedFunction, Object2ObjectFunctions.UnmodifiableFunction, Object2ObjectLinkedOpenCustomHashMap, Object2ObjectLinkedOpenHashMap, Object2ObjectMaps.EmptyMap, Object2ObjectMaps.Singleton, Object2ObjectMaps.SynchronizedMap, Object2ObjectMaps.UnmodifiableMap, Object2ObjectOpenCustomHashMap, Object2ObjectOpenHashMap, Object2ObjectRBTreeMap, Object2ObjectSortedMaps.EmptySortedMap, Object2ObjectSortedMaps.Singleton, Object2ObjectSortedMaps.SynchronizedSortedMap, Object2ObjectSortedMaps.UnmodifiableSortedMap
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface
public interface Function<K,​V>
extends java.util.function.Function<K,​V>
A function mapping keys into values.

Instances of this class represent functions: the main difference with Map is that functions do not in principle allow enumeration of their domain or range. The need for this interface lies in the existence of several highly optimized implementations of functions (e.g., minimal perfect hashes) which do not actually store their domain or range explicitly. In case the domain is known, containsKey(Object) can be used to perform membership queries.

The choice of naming all methods exactly as in Map makes it possible for all type-specific maps to extend type-specific functions (e.g., Int2IntMap extends Int2IntFunction). However, size() is allowed to return -1 to denote that the number of keys is not available (e.g., in the case of a string hash function).

Note that there is an Object2ObjectFunction that can also set its default return value.

Relationship with Function

This interface predates Java 8's Function and it was conceived with a different purpose. To ease interoperability, we extend Function and implement a default method for apply(Object) that delegates to get(Object). However, while the argument of a Function with keys of type T is of type T, the argument of get(Object) is unparameterized (see the example below).

No attempt will be made at creating type-specific versions of Function as the JDK already provides several specializations, such as IntToLongFunction. Rather, type-specific versions of this class do implement the corresponding classes in java.util.function: for example, Int2LongFunction extends IntToLongFunction and Int2IntFunction extends IntUnaryOperator. For functions that do not have a corresponding JDK function we extend the closest possible function (widening input and output types): for example, Byte2CharFunction extends IntUnaryOperator.

Default methods and lambda expressions

All optional operations have default methods throwing an UnsupportedOperationException, except for containsKey(Object), which returns true, and size(), which return -1. Thus, it is possible to define an instance of this class using a lambda expression that will specify get(Object). Note that the type signature of get(Object) might lead to slightly counter-intuitive behaviour. For example, to define the identity function on Integer objects you need to write

     it.unimi.dsi.fastutil.Function<Integer, Integer> f = (x) -> (Integer)x;
 
as the argument to get(Object) is unparameterized.

Warning: Equality of functions is not specified by contract, and it will usually be by reference, as there is no way to enumerate the keys and establish whether two functions represent the same mathematical entity.

See Also:
Map, Function
  • Method Summary

    Modifier and Type Method Description
    default V apply​(K key)
    This is equivalent to calling get(Object).
    default void clear()
    Removes all associations from this function (optional operation).
    default boolean containsKey​(java.lang.Object key)
    Returns true if this function contains a mapping for the specified key.
    V get​(java.lang.Object key)
    Returns the value associated by this function to the specified key.
    default V getOrDefault​(java.lang.Object key, V defaultValue)
    Returns the value associated by this function to the specified key, or give the specified value if not present.
    default V put​(K key, V value)
    Associates the specified value with the specified key in this function (optional operation).
    default V remove​(java.lang.Object key)
    Removes this key and the associated value from this function if it is present (optional operation).
    default int size()
    Returns the intended number of keys in this function, or -1 if no such number exists.

    Methods inherited from interface java.util.function.Function

    andThen, compose
  • Method Details

    • apply

      default V apply​(K key)
      This is equivalent to calling get(Object).
      Specified by:
      apply in interface java.util.function.Function<K,​V>
      Parameters:
      key -
      Returns:
      Since:
      8.0.0
      See Also:
      Function.apply(Object), get(Object)
    • put

      default V put​(K key, V value)
      Associates the specified value with the specified key in this function (optional operation).
      Parameters:
      key - the key.
      value - the value.
      Returns:
      the old value, or null if no value was present for the given key.
      See Also:
      Map.put(Object,Object)
    • get

      V get​(java.lang.Object key)
      Returns the value associated by this function to the specified key.
      Parameters:
      key - the key.
      Returns:
      the corresponding value, or null if no value was present for the given key.
      See Also:
      Map.get(Object)
    • getOrDefault

      default V getOrDefault​(java.lang.Object key, V defaultValue)
      Returns the value associated by this function to the specified key, or give the specified value if not present.
      Parameters:
      key - the key.
      defaultValue - the default value to return if not present.
      Returns:
      the corresponding value, or defaultValue if no value was present for the given key.
      Since:
      8.5.0
      See Also:
      Map.getOrDefault(Object, Object)
    • containsKey

      default boolean containsKey​(java.lang.Object key)
      Returns true if this function contains a mapping for the specified key.

      Note that for some kind of functions (e.g., hashes) this method will always return true. This default implementation, in particular, always return true.

      Parameters:
      key - the key.
      Returns:
      true if this function associates a value to key.
      See Also:
      Map.containsKey(Object)
    • remove

      default V remove​(java.lang.Object key)
      Removes this key and the associated value from this function if it is present (optional operation).
      Parameters:
      key - the key.
      Returns:
      the old value, or null if no value was present for the given key.
      See Also:
      Map.remove(Object)
    • size

      default int size()
      Returns the intended number of keys in this function, or -1 if no such number exists.

      Most function implementations will have some knowledge of the intended number of keys in their domain. In some cases, however, this might not be possible. This default implementation, in particular, returns -1.

      Returns:
      the intended number of keys in this function, or -1 if that number is not available.
    • clear

      default void clear()
      Removes all associations from this function (optional operation).
      See Also:
      Map.clear()