Package org.spiderwiz.zutils
Class ZHashMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
org.spiderwiz.zutils.ZHashMap<K,V>
- Type Parameters:
K
- type of keyV
- type of value
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<K,V>
- Direct Known Subclasses:
ZDictionary
public class ZHashMap<K,V> extends HashMap<K,V>
A synchronized implementation of
HashMap
.
The Java implementation of HashMap
is unsynchronized. This class extends the implementation to provide
synchronization.
- See Also:
- Serialized Form
-
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
-
Method Summary
Modifier and Type Method Description void
clear()
Synchronized implementation ofHashMap.clear()
.V
computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
Synchronized implementation ofHashMap.computeIfAbsent(java.lang.Object, java.util.function.Function)
.boolean
containsKey(Object key)
Synchronized implementation ofHashMap.containsKey(java.lang.Object)
.void
forEach(BiConsumer<? super K,? super V> action)
Synchronized implementation ofHashMap.forEach(java.util.function.BiConsumer)
V
get(Object key)
Synchronized implementation ofHashMap.get(java.lang.Object)
.void
lockRead()
Locks the map for reading.void
lockWrite()
Locks the map for writing.V
put(K key, V value)
Synchronized implementation ofHashMap.put(java.lang.Object, java.lang.Object)
.void
putAll(Map<? extends K,? extends V> m)
Synchronized implementation ofHashMap.putAll(java.util.Map)
.V
putIfAbsent(K key, V value)
Synchronized implementation ofHashMap.putIfAbsent(java.lang.Object, java.lang.Object)
.V
putIfAbsentReturnNew(K key, V value)
This does whatHashMap.putIfAbsent(java.lang.Object, java.lang.Object)
does (plus synchronization), but it always returns the newly stored value.V
remove(Object key)
Synchronized implementation ofHashMap.remove(java.lang.Object)
.boolean
removeAll(Collection<V> c)
Removes a collection of values from the map.boolean
removeIf(BiFunction<? super K,? super V,Boolean> filter)
Removes all of the elements of this map that satisfy the given two-argument predicate, assuming the first argument is the element key and the second is the element value.int
size()
Synchronized implementation ofHashMap.size()
.void
unlockRead()
Unlocks the map after reading.void
unlockWrite()
Unlocks the map after writing.Methods inherited from class java.util.HashMap
clone, compute, computeIfPresent, containsValue, entrySet, getOrDefault, isEmpty, keySet, merge, remove, replace, replace, replaceAll, values
-
Constructor Details
-
Method Details
-
lockRead
public final void lockRead()Locks the map for reading.This method is used by the class internally to provide synchronization, but you can use it directly when you need to perform a synchronized bulk read operation on the map.
-
lockWrite
public final void lockWrite()Locks the map for writing.This method is used by the class internally to provide synchronization, but you can use it directly when you need to perform a synchronized bulk write operation on the map.
-
unlockRead
public final void unlockRead()Unlocks the map after reading.This method is used by the class internally, but you should use it directly if you use
lockRead()
directly. -
unlockWrite
public final void unlockWrite()Unlocks the map after writing.This method is used by the class internally, but you should use it directly if you use
lockWrite()
directly. -
get
Synchronized implementation ofHashMap.get(java.lang.Object)
. -
put
Synchronized implementation ofHashMap.put(java.lang.Object, java.lang.Object)
. -
putAll
Synchronized implementation ofHashMap.putAll(java.util.Map)
. -
putIfAbsent
Synchronized implementation ofHashMap.putIfAbsent(java.lang.Object, java.lang.Object)
.- Specified by:
putIfAbsent
in interfaceMap<K,V>
- Overrides:
putIfAbsent
in classHashMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the specified key, or null if there was no mapping for the key. (A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.)
-
computeIfAbsent
Synchronized implementation ofHashMap.computeIfAbsent(java.lang.Object, java.util.function.Function)
.- Specified by:
computeIfAbsent
in interfaceMap<K,V>
- Overrides:
computeIfAbsent
in classHashMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedmappingFunction
- the mapping function to compute a value- Returns:
- the current (existing or computed) value associated with the specified key, or null if the computed value is null
-
putIfAbsentReturnNew
This does whatHashMap.putIfAbsent(java.lang.Object, java.lang.Object)
does (plus synchronization), but it always returns the newly stored value.- Parameters:
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.- Returns:
- the newly stored value.
-
remove
Synchronized implementation ofHashMap.remove(java.lang.Object)
. -
removeAll
Removes a collection of values from the map.- Parameters:
c
- collection containing values to be removed from this map.- Returns:
- true if this map changed as a result of the call.
-
removeIf
Removes all of the elements of this map that satisfy the given two-argument predicate, assuming the first argument is the element key and the second is the element value.- Parameters:
filter
- a two-argument predicate which returnstrue
for elements whose key matches the first argument and value matches the second argument to be removed- Returns:
true
if any elements were removed
-
size
public int size()Synchronized implementation ofHashMap.size()
. -
containsKey
Synchronized implementation ofHashMap.containsKey(java.lang.Object)
.- Specified by:
containsKey
in interfaceMap<K,V>
- Overrides:
containsKey
in classHashMap<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.
-
clear
public void clear()Synchronized implementation ofHashMap.clear()
. -
forEach
Synchronized implementation ofHashMap.forEach(java.util.function.BiConsumer)
-