K
- the type of keys maintained by this mapV
- the type of mapped values@ThreadSafe public class ConcurrentIdentityHashMap<K,V> extends Object implements ConcurrentMap<K,V>
ConcurrentHashMap
that utilizes identity semantics
with its keys
Having identity semantics means that it is possible to have two objects with which are
equal based on the object's equals()
implementation, but not be overridden in the hash
map if you try to put both. For example:
ConcurrentIdentityHashMap<String, Boolean> map = new ConcurrentIdentityHashMap<>(); String s1 = new String("test"); String s2 = new String("test"); map.put(s1, true); map.put(s2, false); map.size(); // Prints "2". map.get(s1); // true map.get(s2); // falseKeys in this mapped are hashed based on the identity, that is, the location in memory of the objects rather than the equality which java typically uses for comparison. This implementation of
ConcurrentMap
has a few limitations which the corresponding
ConcurrentHashMap
does not have.
- This map's entrySet()
returns a copy of the entries at the time of calling the method.
The returned set does will not receive updates from the underlying map. This is a departure
from the behavior of ConcurrentHashMap
- This map's keySet()
does return the map-backed keySet and provides the same
behavior as in ConcurrentHashMap
, however the difference is that Set.toArray()
and Set.toArray(Object[])
methods are left unimplemented and will throw an error if
the user attempts to convert the set into an array.Constructor and Description |
---|
ConcurrentIdentityHashMap()
Creates a new, empty map with the default initial table size (16).
|
ConcurrentIdentityHashMap(int initialCapacity)
Creates a new, empty map with an initial table size accommodating the specified number of
elements without having to dynamically resize.
|
ConcurrentIdentityHashMap(int initialCapacity,
float loadFactor)
Creates a new, empty map with an initial table size based on the given number of elements
(
initialCapacity ) and initial load factor (loadFactor ) with a
concurrencyLevel of 1. |
ConcurrentIdentityHashMap(int initialCapacity,
float loadFactor,
int concurrencyLevel)
Creates a new, empty map with an initial table size based on the given number of elements
(
initialCapacity ), load factor (loadFactor ), and number of
expected concurrently updating threads (concurrencyLevel ). |
Modifier and Type | Method and Description |
---|---|
void |
clear() |
boolean |
containsKey(Object k) |
boolean |
containsValue(Object value) |
Set<Map.Entry<K,V>> |
entrySet()
Returns a set representing the entries of this map at the time of calling.
|
V |
get(Object k) |
boolean |
isEmpty() |
Set<K> |
keySet()
Returns a set representing all keys contained within the map.
|
V |
put(K k,
V v) |
void |
putAll(Map<? extends K,? extends V> map) |
V |
putIfAbsent(K k,
V v) |
V |
remove(Object k) |
boolean |
remove(Object k,
Object v) |
V |
replace(K k,
V v) |
boolean |
replace(K k,
V v1,
V v2) |
int |
size() |
Collection<V> |
values() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
public ConcurrentIdentityHashMap()
public ConcurrentIdentityHashMap(int initialCapacity)
initialCapacity
- The implementation performs internal sizing to accommodate this many
elements.IllegalArgumentException
- if the initial capacity of
elements is negativepublic ConcurrentIdentityHashMap(int initialCapacity, float loadFactor)
initialCapacity
) and initial load factor (loadFactor
) with a
concurrencyLevel
of 1.initialCapacity
- the initial capacity. The implementation performs internal sizing to
accommodate this many elements, given the specified load factor.loadFactor
- the load factor (table density) for
establishing the initial table sizeIllegalArgumentException
- if the initial capacity of
elements is negative or the load factor is nonpositivepublic ConcurrentIdentityHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
initialCapacity
), load factor (loadFactor
), and number of
expected concurrently updating threads (concurrencyLevel
).initialCapacity
- the initial capacity. The implementation performs internal sizing to
accommodate this many elements, given the specified load factor.loadFactor
- the load factor (table density) for
establishing the initial table sizeconcurrencyLevel
- the estimated number of concurrently updating threads. The
implementation may use this value as a sizing hint.IllegalArgumentException
- if the initial capacity is negative or the load factor or
concurrencyLevel are nonpositivepublic boolean containsKey(Object k)
containsKey
in interface Map<K,V>
public V putIfAbsent(K k, V v)
putIfAbsent
in interface ConcurrentMap<K,V>
putIfAbsent
in interface Map<K,V>
public Set<Map.Entry<K,V>> entrySet()
ConcurrentHashMap
says that the entry set backed by the map is
updated when the underlying map is updated. That is not the case for this class.public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
public Set<K> keySet()
ConcurrentHashMap
implementation
by providing its own implementation of a Set for IdentityObject
. Not all methods
have been implemented. Namely the toArray
, add*
, and
*all(Collection<?> c)
methods.Copyright © 2023. All Rights Reserved.