- Type Parameters:
K-T-
The same (key,value) pair can be added to the map several times and then removed several times. A reference count is incremented for each addition and, provided the count is greater than 0, decremented on removal.
The pair is removed from the map when a remove decrements the reference count to 0.
This class is thread safe.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a key value pair to the map.voidclear()Clear the map of all keys regardless of reference counts.booleanbooleancontainsKey(Object key) Return the value associated with a key in the map.booleanisEmpty()keys()keySet()intReturn the reference count for the entry corresponding to a key in the map.voidDecrement the reference count for a key, and remove the corresponding entry from the map if the result is 0.voidRemove the entry corresponding to the key from the map completely.intsize()
-
Constructor Details
-
RefCountingMap
public RefCountingMap()
-
-
Method Details
-
contains
-
keys
-
size
public int size() -
isEmpty
public boolean isEmpty() -
clear
public void clear()Clear the map of all keys regardless of reference counts. -
keySet
-
containsKey
-
add
Add a key value pair to the map.if there is no entry in the map for the key, then a key value pair is added to the map with a reference count of 1.
If there is already an entry in the map for the same key and value, the reference count for that entry is incremented.
if there is an entry in the map for the key, but with a different value, then that entry is replaced with a new entry for the key and value with a reference count of 1.
- Parameters:
key-value-
-
remove
Decrement the reference count for a key, and remove the corresponding entry from the map if the result is 0.Do nothing if there is no entry in the map corresponding to the key.
- Parameters:
key-
-
removeAll
Remove the entry corresponding to the key from the map completely.This method ignores the reference count.
- Parameters:
key-
-
refCount
Return the reference count for the entry corresponding to a key in the map.Returns 0 if there is no entry in the map corresponding to the key.
- Parameters:
key-- Returns:
- the reference count for the entry corresponding to key in the map, or 0 if there is no corresponding entry.
-
get
Return the value associated with a key in the map.- Parameters:
key-- Returns:
- the value associated with the key, or null if there is no such value.
-