Package com.cedarsoftware.util
Class MultiKeyMap<V>
java.lang.Object
com.cedarsoftware.util.MultiKeyMap<V>
- Type Parameters:
V
- the type of values stored in the map
- All Implemented Interfaces:
ConcurrentMap<Object,
,V> Map<Object,
V>
High-performance N-dimensional key-value Map implementation - the definitive solution for multi-dimensional lookups.
MultiKeyMap allows storing and retrieving values using multiple keys. Unlike traditional maps that use a single key, this map can handle keys with any number of components, making it ideal for complex lookup scenarios like user permissions, configuration trees, and caching systems.
Key Features:
- N-Dimensional Keys: Support for keys with any number of components (1, 2, 3, ... N).
- High Performance: Zero-allocation polymorphic storage, MurmurHash3 finalization, and optimized hash computation — no GC/heap pressure for gets in flat cases.
- Thread-Safe: Lock-free reads with auto-tuned stripe locking that scales with your server cores, similar to ConcurrentHashMap.
- Map Interface Compatible: Supports single-key operations via the standard Map interface (get()/put() automatically unpack Collections/Arrays into multi-keys).
- Flexible API: Var-args methods for convenient multi-key operations (getMultiKey()/putMultiKey() with many keys).
- Smart Collection Handling: Configurable behavior for Collections via
MultiKeyMap.CollectionKeyMode
— change the default automatic unpacking capability as needed. - N-Dimensional Array Expansion: Nested arrays of any depth are automatically flattened recursively into multi-keys, with configurable structure preservation.
- Cross-Container Equivalence: Arrays and Collections with equivalent structure are treated as identical keys, regardless of container type.
Dimensional Behavior Control:
MultiKeyMap provides revolutionary control over how dimensions are handled through the flattenDimensions
parameter:
- Structure-Preserving Mode (default, flattenDimensions = false): Different structural depths remain distinct keys. Arrays/Collections with different nesting levels create separate entries.
- Dimension-Flattening Mode (flattenDimensions = true): All equivalent flat representations are treated as identical keys, regardless of original container structure.
Performance Characteristics:
- Lock-Free Reads: Get operations require no locking for optimal concurrent performance
- Auto-Tuned Stripe Locking: Write operations use stripe locking that adapts to your server's core count
- Zero-Allocation Gets: No temporary objects created during retrieval operations
- MurmurHash3 Finalization: Enhanced hash distribution reduces collisions
- Polymorphic Storage: Efficient memory usage adapts storage format based on key complexity
API Overview:
MultiKeyMap provides two complementary APIs:
- Map Interface: Use as
Map<Object, V>
for compatibility with existing code and single-key operations - MultiKeyMap API: Declare as
MultiKeyMap<V>
to access powerful var-args methods for multi-dimensional operations
Usage Examples:
// Basic multi-dimensional usage
MultiKeyMap<String> map = new MultiKeyMap<>();
map.putMultiKey("user-config", "user123", "settings", "theme");
String theme = map.getMultiKey("user123", "settings", "theme");
// Cross-container equivalence
map.put(new String[]{"key1", "key2"}, "value1"); // Array key
String value = map.get(Arrays.asList("key1", "key2")); // Collection lookup - same key!
// Structure-preserving vs flattening modes
MultiKeyMap<String> structured = new MultiKeyMap<>(false); // Structure-preserving (default)
MultiKeyMap<String> flattened = new MultiKeyMap<>(true); // Dimension-flattening
For comprehensive examples and advanced usage patterns, see the user guide documentation.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Controls how Collections are treated when used as keys in MultiKeyMap.static class
-
Constructor Summary
ConstructorsConstructorDescriptionMultiKeyMap
(boolean flattenDimensions) MultiKeyMap
(int capacity) MultiKeyMap
(int capacity, float loadFactor) MultiKeyMap
(int capacity, float loadFactor, MultiKeyMap.CollectionKeyMode collectionKeyMode) MultiKeyMap
(int capacity, float loadFactor, MultiKeyMap.CollectionKeyMode collectionKeyMode, boolean flattenDimensions) MultiKeyMap
(MultiKeyMap.CollectionKeyMode collectionKeyMode, boolean flattenDimensions) -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
computeIfAbsent
(Object key, Function<? super Object, ? extends V> mappingFunction) computeIfPresent
(Object key, BiFunction<? super Object, ? super V, ? extends V> remappingFunction) boolean
containsKey
(Object key) boolean
containsMultiKey
(Object... keys) boolean
containsValue
(Object value) entries()
entrySet()
boolean
boolean
getMultiKey
(Object... keys) int
hashCode()
boolean
isEmpty()
keySet()
void
void
putIfAbsent
(Object key, V value) putMultiKey
(V value, Object... keys) boolean
removeMultiKey
(Object... keys) boolean
int
size()
toString()
values()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.concurrent.ConcurrentMap
forEach, getOrDefault, replaceAll
-
Constructor Details
-
MultiKeyMap
public MultiKeyMap(int capacity, float loadFactor, MultiKeyMap.CollectionKeyMode collectionKeyMode, boolean flattenDimensions) -
MultiKeyMap
public MultiKeyMap(int capacity, float loadFactor) -
MultiKeyMap
-
MultiKeyMap
public MultiKeyMap() -
MultiKeyMap
public MultiKeyMap(int capacity) -
MultiKeyMap
-
MultiKeyMap
public MultiKeyMap(boolean flattenDimensions)
-
-
Method Details
-
getCollectionKeyMode
-
getFlattenDimensions
public boolean getFlattenDimensions() -
getMultiKey
-
get
-
putMultiKey
-
put
-
containsMultiKey
-
containsKey
- Specified by:
containsKey
in interfaceMap<Object,
V>
-
removeMultiKey
-
remove
-
size
public int size() -
isEmpty
public boolean isEmpty() -
clear
public void clear() -
containsValue
- Specified by:
containsValue
in interfaceMap<Object,
V>
-
keySet
-
values
-
entrySet
-
putAll
-
putIfAbsent
- Specified by:
putIfAbsent
in interfaceConcurrentMap<Object,
V> - Specified by:
putIfAbsent
in interfaceMap<Object,
V>
-
computeIfAbsent
- Specified by:
computeIfAbsent
in interfaceConcurrentMap<Object,
V> - Specified by:
computeIfAbsent
in interfaceMap<Object,
V>
-
computeIfPresent
public V computeIfPresent(Object key, BiFunction<? super Object, ? super V, ? extends V> remappingFunction) - Specified by:
computeIfPresent
in interfaceConcurrentMap<Object,
V> - Specified by:
computeIfPresent
in interfaceMap<Object,
V>
-
compute
-
merge
-
remove
-
replace
-
replace
-
hashCode
public int hashCode() -
equals
-
toString
-
entries
-
printContentionStatistics
public void printContentionStatistics()
-