public interface Function<K,V>
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.
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.
Map
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all associations from this function (optional operation).
|
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.
|
V |
put(K key,
V value)
Associates the specified value with the specified key in this function (optional operation).
|
V |
remove(java.lang.Object key)
Removes this key and the associated value from this function if it is present (optional operation).
|
int |
size()
Returns the intended number of keys in this function, or -1 if no such number exists.
|
V put(K key, V value)
key
- the key.value
- the value.null
if no value was present for the given key.Map.put(Object,Object)
V get(java.lang.Object key)
key
- the key.null
if no value was present for the given key.Map.get(Object)
boolean containsKey(java.lang.Object key)
Note that for some kind of functions (e.g., hashes) this method will always return true.
key
- the key.key
.Map.containsKey(Object)
V remove(java.lang.Object key)
key
- the key.null
if no value was present for the given key.Map.remove(Object)
int size()
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.
void clear()
Map.clear()