Class MultiMap<K,V>
- Type Parameters:
K- The key type of this map.V- The value type of this map.
- All Implemented Interfaces:
Map<K,V>
This class allows multiple maps to be viewed and accessed as if they were merged into a single map, without actually copying the entries. When the same key exists in multiple maps, the value from the first map (in constructor order) is returned.
Features:
- Zero-Copy Composition: No data is copied when creating a MultiMap; it simply wraps the provided maps
- Transparent Access: Accessing entries by key seamlessly searches all underlying maps in order
- First-Wins Semantics: When a key exists in multiple maps, the value from the first map is returned
- Modification Support: Entries can be removed via the iterator's
Iterator.remove()method - Efficient Size Calculation: The size is computed by counting unique keys across all maps
Usage:
Behavior Notes:
- The order of key lookup follows the order of maps as provided in the constructor
- When a key exists in multiple maps,
get(Object)returns the value from the first map containing that key - The underlying maps must not be
null , but can be empty - Modifications via
Iterator.remove()are delegated to the underlying map's entry set iterator - This class does not support
put(Object, Object),remove(Object), orclear()operations - The
size()method recomputes the count of unique keys each time it's called (not cached) - The
entrySet()iterator only returns each key once (first occurrence), even if it exists in multiple maps
Thread Safety:
This class is not inherently thread-safe. If the underlying maps are modified concurrently during iteration or access, the behavior is undefined. Synchronization must be handled externally if needed.
Example - Processing Multiple Configuration Sources:
See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()booleancontainsKey(Object key) Returnstrue if this map contains a mapping for the specified key.booleancontainsValue(Object value) Returnstrue if this map maps one or more keys to the specified value.entrySet()Returns aSetview of the mappings contained in this map.booleanCompares the specified object with this map for equality.Returns the value to which the specified key is mapped, ornull if this map contains no mapping for the key.inthashCode()Returns the hash code value for this map.booleanisEmpty()Returnstrue if this map contains no key-value mappings.voidintsize()Returns the number of unique key-value mappings in this map.toString()Returns a string representation of this MultiMap.values()Returns aCollectionview of the values contained in this map.Methods inherited from class java.util.AbstractMap
clone, keySetMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
MultiMap
Creates a new MultiMap that presents the specified maps as a single unified map.The maps are stored by reference (not copied), so modifications made through the MultiMap's iterator will affect the original maps.
Example:
Map<String, String>
map1 =new LinkedHashMap<>(Map.of("a" ,"1" )); Map<String, String>map2 =new LinkedHashMap<>(Map.of("b" ,"2" )); MultiMap<String, String>multiMap =new MultiMap<>(map1 ,map2 );// multiMap now represents all entries from both maps - Parameters:
maps- Zero or more maps to combine into this map. Must not benull , and no individual map can benull (but maps can be empty).- Throws:
IllegalArgumentException- if the maps array or any map within it isnull .
-
-
Method Details
-
get
Returns the value to which the specified key is mapped, ornull if this map contains no mapping for the key.This method searches the underlying maps in the order they were provided to the constructor. The first map containing the key determines the returned value.
-
containsKey
Returnstrue if this map contains a mapping for the specified key.- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V> - Parameters:
key- The key whose presence in this map is to be tested.- Returns:
true if this map contains a mapping for the specified key.
-
containsValue
Returnstrue if this map maps one or more keys to the specified value.- Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classAbstractMap<K,V> - Parameters:
value- The value whose presence in this map is to be tested.- Returns:
true if this map maps one or more keys to the specified value.
-
entrySet
Returns aSetview of the mappings contained in this map.The returned set is a composite view of all underlying maps. When a key exists in multiple maps, only the entry from the first map (in constructor order) is included.
The iterator supports the
Iterator.remove()operation, which removes the entry from its underlying map. -
size
Returns the number of unique key-value mappings in this map.This method computes the size by counting unique keys across all underlying maps. If a key exists in multiple maps, it is counted only once. The size is recalculated each time this method is called (it is not cached).
Example:
Map<String, String>
map1 = Map.of("a" ,"1" ,"b" ,"2" );// size = 2 Map<String, String>map2 = Map.of("b" ,"3" ,"c" ,"4" );// size = 2 MultiMap<String, String>multiMap =new MultiMap<>(map1 ,map2 );int totalSize =multiMap .size();// Returns: 3 (a, b, c - b is counted only once) -
isEmpty
Returnstrue if this map contains no key-value mappings. -
values
Returns aCollectionview of the values contained in this map.The returned collection is a view of the values from the entries in
entrySet(). When a key exists in multiple maps, only the value from the first map (in constructor order) is included. -
put
-
remove
-
putAll
-
clear
-
toString
Returns a string representation of this MultiMap.The format is
"[{...},{...},...]" where each{...} is the standard standard string representation of each underlying map (as returned byObject.toString()).Example:
Map<String, String>
map1 = Map.of("a" ,"1" ); Map<String, String>map2 = Map.of("b" ,"2" ); MultiMap<String, String>multiMap =new MultiMap<>(map1 ,map2 );multiMap .toString();// Returns: "[{a=1}, {b=2}]" - Overrides:
toStringin classAbstractMap<K,V> - Returns:
- A string representation of this MultiMap.
-
equals
Compares the specified object with this map for equality.Returns
true if the given object is also a map and the two maps represent the same mappings. More formally, two mapsm1 andm2 represent the same mappings ifm1.entrySet().equals(m2.entrySet()) .This implementation compares the entry sets of the two maps.
-
hashCode
Returns the hash code value for this map.The hash code of a map is defined to be the sum of the hash codes of each entry in the map's
entrySet() view. This ensures thatm1.equals(m2) implies thatm1.hashCode()==m2.hashCode() for any two mapsm1 andm2 , as required by the general contract ofObject.hashCode().This implementation computes the hash code from the entry set.
-