Package org.agrona.collections
Class Long2ObjectHashMap<V>
java.lang.Object
org.agrona.collections.Long2ObjectHashMap<V>
- Type Parameters:
V
- type of values stored in theMap
- Direct Known Subclasses:
Long2NullableObjectHashMap
Map
implementation specialised for long keys using open addressing and
linear probing for cache efficient access.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) class
Base iterator implementation that contains basic logic of traversing the element in the backing array.final class
Iterator over entries which supports access to unboxed keys viaLong2ObjectHashMap.EntryIterator.getLongKey()
.final class
Set of entries which supports access via an optionally cached iterator to avoid allocation.final class
Iterator over keys which supports access to unboxed keys viaLong2ObjectHashMap.KeyIterator.nextLong()
.final class
Set of keys which supports optionally cached iterators to avoid allocation.final class
Collection of values which supports optionally cached iterators to avoid allocation.final class
Iterator over values. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Long2ObjectHashMap<V>.EntrySet
private long[]
private Long2ObjectHashMap<V>.KeySet
private final float
(package private) static final int
private int
private final boolean
private int
private Long2ObjectHashMap<V>.ValueCollection
private Object[]
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs map withMIN_CAPACITY
,Hashing.DEFAULT_LOAD_FACTOR
and enables caching of iterators.Long2ObjectHashMap
(int initialCapacity, float loadFactor) Constructs map with given initial capacity and load factory and enables caching of iterators.Long2ObjectHashMap
(int initialCapacity, float loadFactor, boolean shouldAvoidAllocation) Construct a new map allowing a configuration for initial capacity and load factor.Long2ObjectHashMap
(Long2ObjectHashMap<V> mapToCopy) Copy construct a new map from an existing one. -
Method Summary
Modifier and TypeMethodDescriptionint
capacity()
Get the total capacity for the map to which the load factor will be a fraction of.void
clear()
void
compact()
Compact theMap
backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.private void
compactChain
(int deleteIndex) compute
(long key, LongObjectToObjectFunction<? super V, ? extends V> remappingFunction) Attempts to compute a mapping for the specified key and its current mapped value (ornull
if there is no current mapping).computeIfAbsent
(long key, LongFunction<? extends V> mappingFunction) Get a value for a given key, or if it does not exist then default the value via aLongFunction
and put it in the map.computeIfAbsent
(Long key, Function<? super Long, ? extends V> mappingFunction) computeIfPresent
(long key, LongObjectToObjectFunction<? super V, ? extends V> remappingFunction) If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.computeIfPresent
(Long key, BiFunction<? super Long, ? super V, ? extends V> remappingFunction) boolean
containsKey
(long key) Overloaded version ofMap.containsKey(Object)
that takes a primitive long key.boolean
containsKey
(Object key) boolean
containsValue
(Object value) entrySet()
boolean
void
forEach
(BiConsumer<? super Long, ? super V> action) void
forEachLong
(LongObjConsumer<V> consumer) Primitive specialised implementation ofMap.forEach(BiConsumer)
.get
(long key) Overloaded version ofMap.get(Object)
that takes a primitive long key.protected V
getMapped
(long key) Get mapped value without boxing the key.getOrDefault
(long key, V defaultValue) Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.int
hashCode()
private void
boolean
isEmpty()
keySet()
float
Get the load factor beyond which the map will increase size.void
longForEach
(LongObjConsumer<V> consumer) Deprecated.protected Object
mapNullValue
(Object value) Longerceptor for masking null values.Primitive specialised version ofMap.merge(Object, Object, BiFunction)
.Overloaded version ofMap.put(Object, Object)
that takes a primitive long key.void
void
putAll
(Long2ObjectHashMap<? extends V> map) Put all values from the given map longo this one without allocation.putIfAbsent
(long key, V value) Primitive specialised version ofMap.putIfAbsent(Object, Object)
.private void
rehash
(int newCapacity) remove
(long key) Overloaded version ofMap.remove(Object)
that takes a primitive long key.boolean
Primitive specialised version ofMap.remove(Object, Object)
.boolean
Primitive specialised version ofMap.replace(Object, Object)
.boolean
Primitive specialised version ofMap.replace(Object, Object, Object)
.void
replaceAll
(BiFunction<? super Long, ? super V, ? extends V> function) void
replaceAllLong
(LongObjectToObjectFunction<? super V, ? extends V> function) Primitive specialised version ofMap.replaceAll(BiFunction)
.int
Get the actual threshold which when reached the map will resize.int
size()
toString()
protected V
unmapNullValue
(Object value) Longerceptor for unmasking null values.values()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
getOrDefault, putIfAbsent, replace, replace
-
Field Details
-
MIN_CAPACITY
static final int MIN_CAPACITY- See Also:
-
loadFactor
private final float loadFactor -
resizeThreshold
private int resizeThreshold -
size
private int size -
shouldAvoidAllocation
private final boolean shouldAvoidAllocation -
keys
private long[] keys -
values
-
valueCollection
-
keySet
-
entrySet
-
-
Constructor Details
-
Long2ObjectHashMap
public Long2ObjectHashMap()Constructs map withMIN_CAPACITY
,Hashing.DEFAULT_LOAD_FACTOR
and enables caching of iterators. -
Long2ObjectHashMap
public Long2ObjectHashMap(int initialCapacity, float loadFactor) Constructs map with given initial capacity and load factory and enables caching of iterators.- Parameters:
initialCapacity
- for the backing array.loadFactor
- limit for resizing on puts.
-
Long2ObjectHashMap
public Long2ObjectHashMap(int initialCapacity, float loadFactor, boolean shouldAvoidAllocation) Construct a new map allowing a configuration for initial capacity and load factor.- Parameters:
initialCapacity
- for the backing array.loadFactor
- limit for resizing on puts.shouldAvoidAllocation
- should allocation be avoided by caching iterators and map entries.
-
Long2ObjectHashMap
Copy construct a new map from an existing one.- Parameters:
mapToCopy
- for construction.
-
-
Method Details
-
loadFactor
public float loadFactor()Get the load factor beyond which the map will increase size.- Returns:
- load factor for when the map should increase size.
-
capacity
public int capacity()Get the total capacity for the map to which the load factor will be a fraction of.- Returns:
- the total capacity for the map.
-
resizeThreshold
public int resizeThreshold()Get the actual threshold which when reached the map will resize. This is a function of the current capacity and load factor.- Returns:
- the threshold when the map will resize.
-
size
public int size() -
isEmpty
public boolean isEmpty() -
forEach
-
longForEach
Deprecated.UseforEachLong(LongObjConsumer)
instead.UseforEachLong(LongObjConsumer)
instead.- Parameters:
consumer
- a callback called for each key/value pair in the map.- See Also:
-
forEachLong
Primitive specialised implementation ofMap.forEach(BiConsumer)
.NB: Renamed from forEach to avoid overloading on parameter types of lambda expression, which doesn't play well with type inference in lambda expressions.
- Parameters:
consumer
- a callback called for each key/value pair in the map.
-
containsKey
- Specified by:
containsKey
in interfaceMap<Long,
V>
-
containsKey
public boolean containsKey(long key) Overloaded version ofMap.containsKey(Object)
that takes a primitive long key.- Parameters:
key
- for indexing theMap
.- Returns:
- true if the key is found otherwise false.
-
containsValue
- Specified by:
containsValue
in interfaceMap<Long,
V>
-
get
-
get
Overloaded version ofMap.get(Object)
that takes a primitive long key.- Parameters:
key
- for indexing theMap
.- Returns:
- the value if found otherwise null.
-
getOrDefault
Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.- Parameters:
key
- whose associated value is to be returned.defaultValue
- the default mapping of the key.- Returns:
- the value to which the specified key is mapped, or
defaultValue
if this map contains no mapping for the key.
-
getMapped
Get mapped value without boxing the key.- Parameters:
key
- to get value by.- Returns:
- mapped value or
null
.
-
computeIfAbsent
- Specified by:
computeIfAbsent
in interfaceMap<Long,
V>
-
computeIfAbsent
Get a value for a given key, or if it does not exist then default the value via aLongFunction
and put it in the map.Primitive specialized version of
Map.computeIfAbsent(Object, Function)
.- Parameters:
key
- to search on.mappingFunction
- to provide a value if the get returns null.- Returns:
- the value if found otherwise the default.
-
computeIfPresent
public V computeIfPresent(Long key, BiFunction<? super Long, ? super V, ? extends V> remappingFunction) - Specified by:
computeIfPresent
in interfaceMap<Long,
V>
-
computeIfPresent
public V computeIfPresent(long key, LongObjectToObjectFunction<? super V, ? extends V> remappingFunction) If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.If the function returns
null
, the mapping is removed.Primitive specialized version of
Map.computeIfPresent(Object, BiFunction)
.- Parameters:
key
- to search on.remappingFunction
- to provide a value if the get returns missingValue.- Returns:
- the new value associated with the specified key, or
null
if none.
-
compute
-
compute
Attempts to compute a mapping for the specified key and its current mapped value (ornull
if there is no current mapping).If the function returns
null
, the mapping is removed (or remains absent if initially absent).Primitive specialized version of
Map.compute(Object, BiFunction)
.- Parameters:
key
- to search on.remappingFunction
- to provide a value if the get returns missingValue.- Returns:
- the new value associated with the specified key, or
null
if none.
-
merge
-
merge
Primitive specialised version ofMap.merge(Object, Object, BiFunction)
.- Parameters:
key
- with which the resulting value is to be associated.value
- the non-null value to be merged with the existing value associated with the key or, if no existing value or a null value is associated with the key, to be associated with the key.remappingFunction
- the function to recompute a value if present.- Returns:
- the new value associated with the specified key, or null if no value is associated with the key.
-
put
-
put
Overloaded version ofMap.put(Object, Object)
that takes a primitive long key. -
remove
-
remove
Overloaded version ofMap.remove(Object)
that takes a primitive long key.- Parameters:
key
- for indexing theMap
.- Returns:
- the value if found otherwise null.
-
remove
-
remove
Primitive specialised version ofMap.remove(Object, Object)
.- Parameters:
key
- with which the specified value is associated.value
- expected to be associated with the specified key.- Returns:
true
if the value was removed.
-
clear
public void clear() -
compact
public void compact()Compact theMap
backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor. -
putAll
-
putAll
Put all values from the given map longo this one without allocation.- Parameters:
map
- whose value are to be added.
-
putIfAbsent
Primitive specialised version ofMap.putIfAbsent(Object, Object)
.- Parameters:
key
- with which the specified value is to be associated.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.
-
keySet
-
values
-
entrySet
-
toString
-
equals
-
hashCode
public int hashCode() -
mapNullValue
Longerceptor for masking null values.- Parameters:
value
- value to mask.- Returns:
- masked value.
-
unmapNullValue
Longerceptor for unmasking null values.- Parameters:
value
- value to unmask.- Returns:
- unmasked value.
-
replace
Primitive specialised version ofMap.replace(Object, Object)
.- Parameters:
key
- key with which the specified value is associated.value
- 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.
-
replace
Primitive specialised version ofMap.replace(Object, Object, Object)
.- Parameters:
key
- key with which the specified value is associated.oldValue
- value expected to be associated with the specified key.newValue
- value to be associated with the specified key.- Returns:
true
if the value was replaced.
-
replaceAll
- Specified by:
replaceAll
in interfaceMap<Long,
V>
-
replaceAllLong
Primitive specialised version ofMap.replaceAll(BiFunction)
.NB: Renamed from replaceAll to avoid overloading on parameter types of lambda expression, which doesn't play well with type inference in lambda expressions.
- Parameters:
function
- the function to apply to each entry.
-
increaseCapacity
private void increaseCapacity() -
rehash
private void rehash(int newCapacity) -
compactChain
private void compactChain(int deleteIndex)
-
forEachLong(LongObjConsumer)
instead.