public class Map<K,V> extends IMap.Mixin<K,V>
PersistentHashMap
for lookups and construction for collections smaller than 100k entries, and has
equivalent performance for larger collections.
By ensuring that equivalent maps always have equivalent layout in memory, it can perform equality checks and set operations (union, difference, intersection) significantly faster than a more naive implementation. By keeping the memory layout of each node more compact, iteration is at least 2x faster than Clojure's implementation.
IMap.Durable<K,V>, IMap.Mixin<K,V>
Modifier and Type | Field and Description |
---|---|
static Map |
EMPTY |
hash
Constructor and Description |
---|
Map() |
Map(ToLongFunction<K> hashFn,
BiPredicate<K,K> equalsFn)
Creates a map.
|
Modifier and Type | Method and Description |
---|---|
Map<K,V> |
clone() |
boolean |
contains(K key) |
Map<K,V> |
difference(IMap<K,?> m) |
Map<K,V> |
difference(ISet<K> keys) |
static <K,V> Map<K,V> |
empty() |
boolean |
equals(IMap<K,V> m,
BiPredicate<V,V> valEquals) |
boolean |
equals(Object obj) |
Map<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> Map<K,V> |
from(IList<IEntry<K,V>> entries) |
static <K,V> Map<K,V> |
from(IMap<K,V> map) |
static <K,V> Map<K,V> |
from(Iterator<IEntry<K,V>> entries) |
static <K,V> Map<K,V> |
from(Map<K,V> map) |
V |
get(K key,
V defaultValue) |
OptionalLong |
indexOf(K key) |
Map<K,V> |
intersection(IMap<K,?> m) |
Map<K,V> |
intersection(ISet<K> keys) |
boolean |
isLinear() |
Iterator<IEntry<K,V>> |
iterator() |
BiPredicate<K,K> |
keyEquality() |
ToLongFunction<K> |
keyHash() |
Set<K> |
keys() |
Map<K,V> |
linear()
This returns a data structure which is linear, or temporarily mutable.
|
<U> Map<K,U> |
mapValues(BiFunction<K,V,U> f) |
Map<K,V> |
merge(IMap<K,V> m,
BinaryOperator<V> mergeFn) |
IEntry<K,V> |
nth(long idx) |
Map<K,V> |
put(K key,
V value,
BinaryOperator<V> merge) |
Map<K,V> |
put(K key,
V value,
BinaryOperator<V> merge,
Object editor) |
Map<K,V> |
remove(K key) |
Map<K,V> |
remove(K key,
Object editor) |
long |
size() |
List<Map<K,V>> |
split(int parts)
Splits the collection into roughly even pieces, for parallel processing.
|
Map<K,V> |
union(IMap<K,V> m) |
Map<K,V> |
update(K key,
UnaryOperator<V> update) |
Map<K,V> |
update(K key,
UnaryOperator<V> update,
Object editor) |
hashCode, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
apply, containsAll, containsAll, containsAny, containsAny, diff, entries, get, getOrCreate, hashSortedEntries, iterator, put, save, sliceIndices, spliterator, stream, toMap, values
nth
public static final Map EMPTY
public Map(ToLongFunction<K> hashFn, BiPredicate<K,K> equalsFn)
hashFn
- a function which yields the hash value of keysequalsFn
- a function which checks equality of keyspublic Map()
public static <K,V> Map<K,V> from(IMap<K,V> map)
map
- another mappublic static <K,V> Map<K,V> from(Map<K,V> map)
map
public static <K,V> Map<K,V> from(Iterator<IEntry<K,V>> entries)
entries
- an iterator of IEntry
objectspublic static <K,V> Map<K,V> from(IList<IEntry<K,V>> entries)
entries
- a list of IEntry
objectspublic static <K,V> Map<K,V> empty()
public ToLongFunction<K> keyHash()
public BiPredicate<K,K> keyEquality()
public V get(K key, V defaultValue)
key
, or defaultValue
if there is no such keypublic Map<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 Map<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 boolean contains(K key)
key
is in the map, false otherwisepublic OptionalLong indexOf(K key)
key
within the collection, if it's presentpublic Map<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 Map<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 <U> Map<K,U> mapValues(BiFunction<K,V,U> f)
U
- the new type of the valuesf
- a function which transforms the valuespublic List<Map<K,V>> split(int parts)
ICollection
parts
subsets.parts
- the target number of piecespublic Map<K,V> union(IMap<K,V> m)
m
shadowing those in this amppublic Map<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 Map<K,V> difference(ISet<K> keys)
keys
public Map<K,V> intersection(ISet<K> keys)
keys
public Map<K,V> difference(IMap<K,?> m)
m
public Map<K,V> intersection(IMap<K,?> m)
m
public long size()
public boolean isLinear()
public boolean equals(IMap<K,V> m, BiPredicate<V,V> valEquals)
m
- another mapvalEquals
- a predicate which checks value equalitiespublic boolean equals(Object obj)
equals
in class IMap.Mixin<K,V>