- java.lang.Object
-
- fr.dufrenoy.util.UnsynchronizedMultiMap<K,V>
-
- Type Parameters:
K- the type of keys at this levelV- the type of values at this level (may be anotherMultiMap)
- All Implemented Interfaces:
MultiMap<K,V>,Serializable
public class UnsynchronizedMultiMap<K,V> extends Object implements MultiMap<K,V>, Serializable
A non-thread-safe implementation ofMultiMapbacked by aHashMap. EachUnsynchronizedMultiMap<K, V>associates keys of typeKto values of typeV, whereVmay itself be anotherMultiMap, enabling multi-level key hierarchies.Iterators returned by the view methods (
keySet(),values(),entrySet()) are fail-fast: if the map is structurally modified after the iterator is created, the iterator throwsConcurrentModificationException.Null keys and null values are forbidden. All methods that accept keys or values throw
NullPointerExceptionif a null argument is passed.This implementation is not thread-safe. For concurrent access, use
SynchronizedMultiMap.- See Also:
MultiMap,SynchronizedMultiMap, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description UnsynchronizedMultiMap()Creates a new emptyUnsynchronizedMultiMapwith default initial capacity.UnsynchronizedMultiMap(int initialCapacity)Creates a new emptyUnsynchronizedMultiMapwith the specified initial capacity.UnsynchronizedMultiMap(MultiMap<? extends K,? extends V> source)Creates a newUnsynchronizedMultiMapcontaining the same mappings as the specifiedMultiMap.UnsynchronizedMultiMap(Map<? extends K,? extends V> source)Creates a newUnsynchronizedMultiMapcontaining the same mappings as the specified map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all mappings from this map.booleancontainsKey(K key)Returnstrueif this map contains a mapping for the specified key.Set<Map.Entry<K,V>>entrySet()Returns aSetview of the mappings contained in this map.booleanequals(Object obj)Vget(K key)Returns the value associated with the specified key, ornullif no mapping exists.Optional<V>getOpt(K key)Returns the value associated with the specified key wrapped in anOptional, or an emptyOptionalif no mapping exists.VgetOrCreate(K key, Supplier<V> factory)Returns the value associated with the specified key.inthashCode()booleanisEmpty()Returnstrueif this map contains no mappings.Set<K>keySet()Returns aSetview of the keys contained in this map.Vput(K key, V value)Associates the specified value with the specified key.Vremove(K key)Removes the mapping for the specified key, if present.intsize()Returns the number of key-value mappings at this level.StringtoString()Collection<V>values()Returns aCollectionview of the values contained in this map.
-
-
-
Constructor Detail
-
UnsynchronizedMultiMap
public UnsynchronizedMultiMap()
Creates a new emptyUnsynchronizedMultiMapwith default initial capacity.
-
UnsynchronizedMultiMap
public UnsynchronizedMultiMap(int initialCapacity)
Creates a new emptyUnsynchronizedMultiMapwith the specified initial capacity.- Parameters:
initialCapacity- the initial capacity- Throws:
IllegalArgumentException- ifinitialCapacityis negative
-
UnsynchronizedMultiMap
public UnsynchronizedMultiMap(Map<? extends K,? extends V> source)
Creates a newUnsynchronizedMultiMapcontaining the same mappings as the specified map. Null keys and null values in the source map cause aNullPointerException.- Parameters:
source- the map whose mappings are to be copied- Throws:
NullPointerException- ifsourceis null, or if it contains null keys or null values
-
UnsynchronizedMultiMap
public UnsynchronizedMultiMap(MultiMap<? extends K,? extends V> source)
Creates a newUnsynchronizedMultiMapcontaining the same mappings as the specifiedMultiMap.- Parameters:
source- the multi-map whose mappings are to be copied- Throws:
NullPointerException- ifsourceis null
-
-
Method Detail
-
get
public V get(K key)
Description copied from interface:MultiMapReturns the value associated with the specified key, ornullif no mapping exists. This method is designed for chained lookups across multiple levels.
-
getOpt
public Optional<V> getOpt(K key)
Description copied from interface:MultiMapReturns the value associated with the specified key wrapped in anOptional, or an emptyOptionalif no mapping exists.
-
getOrCreate
public V getOrCreate(K key, Supplier<V> factory)
Description copied from interface:MultiMapReturns the value associated with the specified key. If no mapping exists, creates one using the given factory, inserts it, and returns the new value. This method is designed for chained writes across multiple levels.- Specified by:
getOrCreatein interfaceMultiMap<K,V>- Parameters:
key- the key whose associated value is to be returned or createdfactory- the supplier used to create a new value if absent- Returns:
- the existing or newly created value
-
containsKey
public boolean containsKey(K key)
Description copied from interface:MultiMapReturnstrueif this map contains a mapping for the specified key.- Specified by:
containsKeyin interfaceMultiMap<K,V>- Parameters:
key- the key whose presence is to be tested- Returns:
trueif this map contains a mapping forkey
-
put
public V put(K key, V value)
Description copied from interface:MultiMapAssociates the specified value with the specified key. If a mapping already exists for this key, the old value is replaced.
-
remove
public V remove(K key)
Description copied from interface:MultiMapRemoves the mapping for the specified key, if present.
-
clear
public void clear()
Description copied from interface:MultiMapRemoves all mappings from this map.
-
size
public int size()
Description copied from interface:MultiMapReturns the number of key-value mappings at this level.
-
isEmpty
public boolean isEmpty()
Description copied from interface:MultiMapReturnstrueif this map contains no mappings.
-
keySet
public Set<K> keySet()
Description copied from interface:MultiMapReturns aSetview of the keys contained in this map.
-
values
public Collection<V> values()
Description copied from interface:MultiMapReturns aCollectionview of the values contained in this map.
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
Description copied from interface:MultiMapReturns aSetview of the mappings contained in this map. Each element is aMap.Entry<K, V>.
-
-