Class DerivedKeyHashMap<K,V>
java.lang.Object
com.github.andrewoma.dexx.collection.internal.base.AbstractTraversable<Pair<K,V>>
com.github.andrewoma.dexx.collection.internal.base.AbstractIterable<Pair<K,V>>
com.github.andrewoma.dexx.collection.internal.base.AbstractMap<K,V>
com.github.andrewoma.dexx.collection.DerivedKeyHashMap<K,V>
- All Implemented Interfaces:
Iterable<Pair<K,
,V>> Map<K,
,V> Traversable<Pair<K,
,V>> Iterable<Pair<K,
V>>
DerivedKeyHashMap
is a HashMap
variant where the key for the Map
is derived from the value stored.
By deriving the key it is possible to reduce the memory overhead per node in the map (assuming the key is a primitive field in the value object). e.g.
static class Value {
int key;
int value;
}
DerivedKeyHashMap<Integer, Value> map = new DerivedKeyHashMap<Integer, Value>(
new KeyFunction<Integer, Value>() {
public Integer key(Value value) {
return value.key;
}
});
Value value = new Value();
value.key = 1;
value.value = 100;
map = map.put(value.key, value);
The underlying implementation is a port of Scala's HashMap which is an implementation of a
hash array mapped trie.
It uses significantly less memory than Scala's implementation as the leaf nodes are the values themselves (not objects containing keys, values and hashes).
As the key is derived from the value, DerivedKeyHashMap
does not support null
values.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CompactHashMap
<K, V> private final KeyFunction
<K, V> -
Constructor Summary
ConstructorsModifierConstructorDescriptionDerivedKeyHashMap
(@NotNull KeyFunction<K, V> keyFunction) private
DerivedKeyHashMap
(KeyFunction<K, V> keyFunction, CompactHashMap<K, V> compactHashMap) -
Method Summary
Modifier and TypeMethodDescriptionboolean
containsKey
(K key) Returns true if this map contains the specified key.static <K,
V> @NotNull BuilderFactory <Pair<K, V>, DerivedKeyHashMap<K, V>> factory
(KeyFunction<K, V> keyFunction) <U> void
All collection methods can be built upon thisforEach
definition.Returns the value associated with the key ornull
if the no value exists with the key specified.iterator()
@NotNull DerivedKeyHashMap
<K, V> Returns a map with the value specified associated to the key specified.@NotNull DerivedKeyHashMap
<K, V> Returns a map with the value associated with the key removed if it exists.int
size()
Returns the size of the collection.Methods inherited from class com.github.andrewoma.dexx.collection.internal.base.AbstractMap
asMap, equals, hashCode, keys, values
Methods inherited from class com.github.andrewoma.dexx.collection.internal.base.AbstractTraversable
isEmpty, makeString, makeString, to, toArray, toArray, toIndexedList, toSet, toSortedSet, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface com.github.andrewoma.dexx.collection.Traversable
isEmpty, makeString, makeString, to, toArray, toArray, toIndexedList, toSet, toSortedSet
-
Field Details
-
keyFunction
-
compactHashMap
-
-
Constructor Details
-
DerivedKeyHashMap
-
DerivedKeyHashMap
-
-
Method Details
-
factory
@NotNull public static <K,V> @NotNull BuilderFactory<Pair<K,V>, factoryDerivedKeyHashMap<K, V>> (KeyFunction<K, V> keyFunction) -
containsKey
Description copied from interface:Map
Returns true if this map contains the specified key. -
put
Description copied from interface:Map
Returns a map with the value specified associated to the key specified.If value already exists for the key, it will be replaced.
-
get
Description copied from interface:Map
Returns the value associated with the key ornull
if the no value exists with the key specified. -
remove
Description copied from interface:Map
Returns a map with the value associated with the key removed if it exists. -
size
public int size()Description copied from interface:Traversable
Returns the size of the collection.Warning: infinite collections are possible, as are collections that require traversal to calculate the size.
- Specified by:
size
in interfaceTraversable<K>
- Overrides:
size
in classAbstractTraversable<Pair<K,
V>>
-
forEach
Description copied from interface:Traversable
All collection methods can be built upon thisforEach
definition.- Specified by:
forEach
in interfaceTraversable<K>
- Overrides:
forEach
in classAbstractIterable<Pair<K,
V>>
-
iterator
-