public class LinearMap<K,V> extends IMap.Mixin<K,V>
HashMap
for lookups and construction, and superior in the
case of poor hash distribution. Because entries are stored contiguously, performance of clone()
and
iteration is significantly better than HashMap
. Unlike Map
, it can only hold
Integer.MAX_VALUE
elements.
The IMap.entries()
method is O(1) and allows random access, returning an IList that proxies through
to an
underlying array. Partitioning this list is the most efficient way to process the collection in parallel.
However, LinearMap
also exposes O(N) split(int)
and merge(IMap, BinaryOperator)
methods, which despite their asymptotic complexity can be quite fast in practice. The appropriate way to split this
collection will depend on the use case.
IMap.Durable<K,V>, IMap.Mixin<K,V>
Modifier and Type | Field and Description |
---|---|
static int |
MAX_CAPACITY |
hash
Constructor and Description |
---|
LinearMap() |
LinearMap(int initialCapacity) |
LinearMap(int initialCapacity,
ToLongFunction<K> hashFn,
BiPredicate<K,K> equalsFn) |
LinearMap(ToLongFunction<K> hashFn,
BiPredicate<K,K> equalsFn) |
Modifier and Type | Method and Description |
---|---|
LinearMap<K,V> |
clear() |
LinearMap<K,V> |
clone() |
boolean |
containsAll(IMap<K,?> map) |
boolean |
containsAll(ISet<K> set) |
boolean |
containsAny(IMap<K,?> map) |
boolean |
containsAny(ISet<K> set) |
LinearMap<K,V> |
difference(IMap<K,?> m) |
IMap<K,V> |
difference(ISet<K> keys) |
boolean |
equals(Object obj) |
IMap<K,V> |
forked()
This returns a data structure which is forked, which is equivalent to Clojure's persistent
data structures, also sometimes called functional or immutable.
|
static <K,V> LinearMap<K,V> |
from(Collection<Map.Entry<K,V>> collection) |
static <K,V> LinearMap<K,V> |
from(IMap<K,V> map) |
static <K,V> LinearMap<K,V> |
from(Iterator<IEntry<K,V>> iterator) |
static <K,V> LinearMap<K,V> |
from(Map<K,V> map) |
V |
get(K key,
V defaultValue) |
int |
hashCode() |
OptionalLong |
indexOf(K key) |
LinearMap<K,V> |
intersection(IMap<K,?> m) |
LinearMap<K,V> |
intersection(ISet<K> keys) |
boolean |
isLinear() |
BiPredicate<K,K> |
keyEquality() |
ToLongFunction<K> |
keyHash() |
ISet<K> |
keys() |
IMap<K,V> |
linear()
This returns a data structure which is linear, or temporarily mutable.
|
<U> LinearMap<K,U> |
mapValues(BiFunction<K,V,U> f) |
LinearMap<K,V> |
merge(IMap<K,V> m,
BinaryOperator<V> mergeFn) |
IEntry<K,V> |
nth(long index) |
LinearMap<K,V> |
put(K key,
V value) |
LinearMap<K,V> |
put(K key,
V value,
BinaryOperator<V> merge) |
LinearMap<K,V> |
remove(K key) |
long |
size() |
List<LinearMap<K,V>> |
split(int parts)
Splits the collection into roughly even pieces, for parallel processing.
|
LinearMap<K,V> |
union(IMap<K,V> m) |
LinearMap<K,V> |
update(K key,
UnaryOperator<V> update) |
toString
finalize, getClass, notify, notifyAll, wait, wait, wait
apply, contains, diff, entries, equals, get, getOrCreate, hashSortedEntries, iterator, save, sliceIndices, spliterator, stream, toMap, values
iterator, nth
public static final int MAX_CAPACITY
public LinearMap()
public LinearMap(int initialCapacity)
initialCapacity
- the initial capacity of the mappublic LinearMap(ToLongFunction<K> hashFn, BiPredicate<K,K> equalsFn)
hashFn
- a function which yields the hash value of keysequalsFn
- a function which checks equality of keyspublic LinearMap(int initialCapacity, ToLongFunction<K> hashFn, BiPredicate<K,K> equalsFn)
initialCapacity
- the initial capacity of the maphashFn
- a function which yields the hash value of keysequalsFn
- a function which checks equality of keyspublic static <K,V> LinearMap<K,V> from(IMap<K,V> map)
map
, with the same equality semanticspublic static <K,V> LinearMap<K,V> from(Iterator<IEntry<K,V>> iterator)
iterator
public static <K,V> LinearMap<K,V> from(Collection<Map.Entry<K,V>> collection)
collection
public ToLongFunction<K> keyHash()
public BiPredicate<K,K> keyEquality()
public LinearMap<K,V> put(K key, V value, BinaryOperator<V> merge)
merge
- a function which will be invoked if there is a pre-existing value under key
, with the current
value as the first argument and new value as the second, to determine the combined resultvalue
under key
public <U> LinearMap<K,U> mapValues(BiFunction<K,V,U> f)
U
- the new type of the valuesf
- a function which transforms the valuespublic V get(K key, V defaultValue)
key
, or defaultValue
if there is no such keypublic LinearMap<K,V> update(K key, UnaryOperator<V> update)
update
- a function which takes the existing value, or null
if none exists, and returns an updated
value.update(value)
under key
.public OptionalLong indexOf(K key)
key
within the collection, if it's presentpublic long size()
public boolean isLinear()
public IMap<K,V> forked()
ICollection
If only a single function or scope uses the data structure, it can be left as a linear data structure, which can have significant performance benefits.
If the data structure is already forked, it will simply return itself.
public IMap<K,V> linear()
ICollection
If ICollection.forked()
is called on a linear collection, all references to that linear collection
should be discarded.
If the data structure is already linear, it will simply return itself.
public boolean equals(Object obj)
equals
in class IMap.Mixin<K,V>
public int hashCode()
hashCode
in class IMap.Mixin<K,V>
public List<LinearMap<K,V>> split(int parts)
ICollection
parts
subsets.parts
- the target number of piecespublic LinearMap<K,V> union(IMap<K,V> m)
m
shadowing those in this amppublic LinearMap<K,V> merge(IMap<K,V> m, BinaryOperator<V> mergeFn)
m
- another mapmergeFn
- a function which, in the case of key collisions, takes two values and returns the merged resultpublic LinearMap<K,V> difference(IMap<K,?> m)
m
public IMap<K,V> difference(ISet<K> keys)
keys
public LinearMap<K,V> intersection(IMap<K,?> m)
m
public LinearMap<K,V> intersection(ISet<K> keys)
keys
public boolean containsAll(ISet<K> set)
set
public boolean containsAll(IMap<K,?> map)
map
public boolean containsAny(ISet<K> set)
set