Package com.cedarsoftware.util
Class ConcurrentHashMapNullSafe<K,V>
java.lang.Object
com.cedarsoftware.util.AbstractConcurrentNullSafeMap<K,V>
com.cedarsoftware.util.ConcurrentHashMapNullSafe<K,V>
- Type Parameters:
K
- the type of keys maintained by this mapV
- the type of mapped values
- All Implemented Interfaces:
ConcurrentMap<K,
,V> Map<K,
V>
A thread-safe implementation of
ConcurrentMap
that supports
null
keys and null
values by using internal sentinel objects.
ConcurrentHashMapNullSafe
extends AbstractConcurrentNullSafeMap
and uses a
ConcurrentHashMap
as its backing implementation. This class retains all the advantages
of ConcurrentHashMap
(e.g., high concurrency, thread safety, and performance) while
enabling safe handling of null
keys and values.
Key Features
- Thread-safe and highly concurrent.
- Supports
null
keys andnull
values through internal sentinel objects. - Adheres to the
Map
andConcurrentMap
contracts. - Provides multiple constructors to control initial capacity, load factor, and populate from another map.
Usage Example
// Create an empty ConcurrentHashMapNullSafe
ConcurrentHashMapNullSafe<String, String> map = new ConcurrentHashMapNullSafe<>();
map.put(null, "nullKey");
map.put("key", null);
// Populate from another map
Map<String, String> existingMap = Map.of("a", "b", "c", "d");
ConcurrentHashMapNullSafe<String, String> populatedMap = new ConcurrentHashMapNullSafe<>(existingMap);
System.out.println(map.get(null)); // Outputs: nullKey
System.out.println(map.get("key")); // Outputs: null
System.out.println(populatedMap); // Outputs: {a=b, c=d}
- Author:
- John DeRegnaucourt
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. - See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.cedarsoftware.util.AbstractConcurrentNullSafeMap
AbstractConcurrentNullSafeMap.NullSentinel
-
Field Summary
Fields inherited from class com.cedarsoftware.util.AbstractConcurrentNullSafeMap
internalMap
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new, emptyConcurrentHashMapNullSafe
with the default initial capacity (16) and load factor (0.75).ConcurrentHashMapNullSafe
(int initialCapacity) Constructs a new, emptyConcurrentHashMapNullSafe
with the specified initial capacity and default load factor (0.75).ConcurrentHashMapNullSafe
(int initialCapacity, float loadFactor) Constructs a new, emptyConcurrentHashMapNullSafe
with the specified initial capacity and load factor.ConcurrentHashMapNullSafe
(Map<? extends K, ? extends V> m) Constructs a newConcurrentHashMapNullSafe
with the same mappings as the specified map. -
Method Summary
Methods inherited from class com.cedarsoftware.util.AbstractConcurrentNullSafeMap
clear, compute, computeIfAbsent, containsKey, containsValue, entrySet, equals, get, getOrDefault, hashCode, isEmpty, keySet, maskNullKey, maskNullValue, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size, toString, unmaskNullKey, unmaskNullValue, values
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.concurrent.ConcurrentMap
computeIfPresent, forEach, replaceAll
-
Constructor Details
-
ConcurrentHashMapNullSafe
public ConcurrentHashMapNullSafe()Constructs a new, emptyConcurrentHashMapNullSafe
with the default initial capacity (16) and load factor (0.75).This constructor creates a thread-safe map suitable for general-purpose use, retaining the concurrency properties of
ConcurrentHashMap
while supportingnull
keys and values. -
ConcurrentHashMapNullSafe
public ConcurrentHashMapNullSafe(int initialCapacity) Constructs a new, emptyConcurrentHashMapNullSafe
with the specified initial capacity and default load factor (0.75).- Parameters:
initialCapacity
- the initial capacity. The implementation performs internal sizing to accommodate this many elements.- Throws:
IllegalArgumentException
- if the initial capacity is negative
-
ConcurrentHashMapNullSafe
public ConcurrentHashMapNullSafe(int initialCapacity, float loadFactor) Constructs a new, emptyConcurrentHashMapNullSafe
with the specified initial capacity and load factor.- Parameters:
initialCapacity
- the initial capacity. The implementation performs internal sizing to accommodate this many elements.loadFactor
- the load factor threshold, used to control resizing. Resizing may be performed when the average number of elements per bin exceeds this threshold.- Throws:
IllegalArgumentException
- if the initial capacity is negative or the load factor is nonpositive
-
ConcurrentHashMapNullSafe
Constructs a newConcurrentHashMapNullSafe
with the same mappings as the specified map.This constructor copies all mappings from the given map into the new
ConcurrentHashMapNullSafe
. The mappings are inserted in the order returned by the source map'sentrySet
iterator.- Parameters:
m
- the map whose mappings are to be placed in this map- Throws:
NullPointerException
- if the specified map isnull
-