-
- Type Parameters:
K- the type of keys at this levelV- the type of values at this level (may be anotherMultiMap)
- All Known Implementing Classes:
SynchronizedMultiMap,UnsynchronizedMultiMap
public interface MultiMap<K,V>A recursive multi-dimensional map. EachMultiMap<K, V>associates keys of typeKto values of typeV, whereVmay itself be anotherMultiMap, enabling multi-level key hierarchies with heterogeneous key types per dimension.A partial lookup (stopping before the deepest level) returns a sub-map of reduced dimensionality. A complete lookup returns the terminal value.
Example with three levels:
MultiMap<String, MultiMap<String, MultiMap<String, Integer>>> map = ...; // Chained write map.getOrCreate("France", UnsynchronizedMultiMap::new) .getOrCreate("Paris", UnsynchronizedMultiMap::new) .put("Q1", 42); // Chained read (nullable) Integer val = map.get("France").get("Paris").get("Q1"); // Safe read Optional<Integer> safe = map.getOpt("France") .flatMap(m -> m.getOpt("Paris")) .flatMap(m -> m.getOpt("Q1"));This interface does not extend
Mapbecause the recursive semantics (partial lookups,getOrCreate) are incompatible with the fullMapcontract. However, the API intentionally mirrorsMapwhere applicable.Null keys and null values are forbidden. Implementations must reject them with
NullPointerException.Two implementations are provided:
UnsynchronizedMultiMap— not thread-safe, fail-fast iteratorsSynchronizedMultiMap— thread-safe, snapshot-based iterators
- See Also:
UnsynchronizedMultiMap,SynchronizedMultiMap
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidclear()Removes all mappings from this map.booleancontainsKey(K key)Returnstrueif this map contains a mapping for the specified key.Set<Map.Entry<K,V>>entrySet()Returns aSetview of the mappings contained in this map.Vget(K key)Returns the value associated with the specified key, ornullif no mapping exists.Optional<V>getOpt(K key)Returns the value associated with the specified key wrapped in anOptional, or an emptyOptionalif no mapping exists.VgetOrCreate(K key, Supplier<V> factory)Returns the value associated with the specified key.booleanisEmpty()Returnstrueif this map contains no mappings.Set<K>keySet()Returns aSetview of the keys contained in this map.Vput(K key, V value)Associates the specified value with the specified key.Vremove(K key)Removes the mapping for the specified key, if present.intsize()Returns the number of key-value mappings at this level.Collection<V>values()Returns aCollectionview of the values contained in this map.
-
-
-
Method Detail
-
get
V get(K key)
Returns the value associated with the specified key, ornullif no mapping exists. This method is designed for chained lookups across multiple levels.- Parameters:
key- the key whose associated value is to be returned- Returns:
- the value associated with
key, ornull - Throws:
NullPointerException- ifkeyisnull
-
getOpt
Optional<V> getOpt(K key)
Returns the value associated with the specified key wrapped in anOptional, or an emptyOptionalif no mapping exists.- Parameters:
key- the key whose associated value is to be returned- Returns:
- an
Optionalcontaining the value, or empty - Throws:
NullPointerException- ifkeyisnull
-
getOrCreate
V getOrCreate(K key, Supplier<V> factory)
Returns the value associated with the specified key. If no mapping exists, creates one using the given factory, inserts it, and returns the new value. This method is designed for chained writes across multiple levels.- Parameters:
key- the key whose associated value is to be returned or createdfactory- the supplier used to create a new value if absent- Returns:
- the existing or newly created value
- Throws:
NullPointerException- ifkeyorfactoryisnull, or if the factory returnsnull
-
containsKey
boolean containsKey(K key)
Returnstrueif this map contains a mapping for the specified key.- Parameters:
key- the key whose presence is to be tested- Returns:
trueif this map contains a mapping forkey- Throws:
NullPointerException- ifkeyisnull
-
put
V put(K key, V value)
Associates the specified value with the specified key. If a mapping already exists for this key, the old value is replaced.- Parameters:
key- the key with which the value is to be associatedvalue- the value to associate- Returns:
- the previous value associated with
key, ornullif there was no mapping - Throws:
NullPointerException- ifkeyorvalueisnull
-
remove
V remove(K key)
Removes the mapping for the specified key, if present.- Parameters:
key- the key whose mapping is to be removed- Returns:
- the previous value associated with
key, ornullif there was no mapping - Throws:
NullPointerException- ifkeyisnull
-
clear
void clear()
Removes all mappings from this map.
-
size
int size()
Returns the number of key-value mappings at this level.- Returns:
- the number of mappings
-
isEmpty
boolean isEmpty()
Returnstrueif this map contains no mappings.- Returns:
trueif empty
-
keySet
Set<K> keySet()
Returns aSetview of the keys contained in this map.- Returns:
- a set view of the keys
-
values
Collection<V> values()
Returns aCollectionview of the values contained in this map.- Returns:
- a collection view of the values
-
-