Package org.organicdesign.fp.collections
Class PersistentHashMap<K,V>
- java.lang.Object
-
- org.organicdesign.fp.collections.AbstractUnmodIterable<UnmodMap.UnEntry<K,V>>
-
- org.organicdesign.fp.collections.AbstractUnmodMap<K,V>
-
- org.organicdesign.fp.collections.PersistentHashMap<K,V>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<UnmodMap.UnEntry<K,V>>,java.util.Map<K,V>,BaseMap<K,V>,BaseUnsortedMap<K,V>,ImMap<K,V>,Sized,UnmodIterable<UnmodMap.UnEntry<K,V>>,UnmodMap<K,V>,Transformable<UnmodMap.UnEntry<K,V>>
public class PersistentHashMap<K,V> extends AbstractUnmodMap<K,V> implements ImMap<K,V>, java.io.Serializable
Rich Hickey's immutable rendition of Phil Bagwell's Hash Array Mapped Trie. Uses path copying for persistence, HashCollision leaves vs. extended hashing, Node polymorphism vs. conditionals, No sub-tree pools or root-resizing. Any errors are my own (said Rich, but now says Glen 2015-06-06). This file is a derivative work based on a Clojure collection licensed under the Eclipse Public License 1.0 Copyright Rich Hickey. Errors are Glen Peterson's.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPersistentHashMap.MutableHashMap<K,V>-
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K extends java.lang.Object,V extends java.lang.Object>
-
Nested classes/interfaces inherited from interface org.organicdesign.fp.collections.UnmodMap
UnmodMap.UnEntry<K,V>
-
-
Field Summary
Fields Modifier and Type Field Description static PersistentHashMap<java.lang.Object,java.lang.Object>EMPTY
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description PersistentHashMap<K,V>assoc(K key, V val)Returns a new map with the given key/value addedstatic <K,V>
PersistentHashMap<K,V>empty()static <K,V>
PersistentHashMap<K,V>empty(Equator<K> e)static <K,V>
PersistentHashMap.MutableHashMap<K,V>emptyMutable()Works around some type inference limitations of Java 8.static <K,V>
PersistentHashMap.MutableHashMap<K,V>emptyMutable(Equator<K> e)Works around some type inference limitations of Java 8.Option<UnmodMap.UnEntry<K,V>>entry(K key)Returns an option of the key/value pair associated with this keyEquator<K>equator()Returns the Equator used by this map for equals comparisons and hashCodesUnmodIterator<UnmodMap.UnEntry<K,V>>iterator()A one-time use, mutable, not-thread-safe way to get each value of the underling collection in turn.UnmodIterator<K>keyIterator()PersistentHashMap.MutableHashMap<K,V>mutable()Returns a mutable version of this mutable map.static <K,V>
PersistentHashMap<K,V>of(java.lang.Iterable<java.util.Map.Entry<K,V>> kvPairs)Returns a new PersistentHashMap of the given keys and their paired values.static <K,V>
PersistentHashMap<K,V>ofEq(Equator<K> eq, java.lang.Iterable<java.util.Map.Entry<K,V>> es)Returns a new PersistentHashMap of the given keys and their paired values, skipping any null Entries.intsize()Returns the number of items in this collection or iterable.UnmodIterator<V>valIterator()PersistentHashMap<K,V>without(K key)Returns a new map with the given key/value removed-
Methods inherited from class org.organicdesign.fp.collections.AbstractUnmodMap
equals
-
Methods inherited from class org.organicdesign.fp.collections.AbstractUnmodIterable
hashCode, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.organicdesign.fp.collections.BaseMap
containsKey, get, getOrElse
-
Methods inherited from interface org.organicdesign.fp.xform.Transformable
toImList, toImMap, toImRrbt, toImSet, toImSortedMap, toImSortedSet, toMutableList, toMutableMap, toMutableRrbt, toMutableSet, toMutableSortedMap, toMutableSortedSet
-
Methods inherited from interface org.organicdesign.fp.collections.UnmodIterable
concat, drop, dropWhile, filter, flatMap, fold, foldUntil, head, map, precat, take, takeWhile
-
Methods inherited from interface org.organicdesign.fp.collections.UnmodMap
clear, compute, computeIfAbsent, computeIfPresent, containsValue, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, values
-
-
-
-
Field Detail
-
EMPTY
public static final PersistentHashMap<java.lang.Object,java.lang.Object> EMPTY
-
-
Method Detail
-
empty
public static <K,V> PersistentHashMap<K,V> empty()
-
emptyMutable
public static <K,V> PersistentHashMap.MutableHashMap<K,V> emptyMutable()
Works around some type inference limitations of Java 8.
-
empty
public static <K,V> PersistentHashMap<K,V> empty(Equator<K> e)
-
emptyMutable
public static <K,V> PersistentHashMap.MutableHashMap<K,V> emptyMutable(Equator<K> e)
Works around some type inference limitations of Java 8.
-
ofEq
public static <K,V> PersistentHashMap<K,V> ofEq(Equator<K> eq, java.lang.Iterable<java.util.Map.Entry<K,V>> es)
Returns a new PersistentHashMap of the given keys and their paired values, skipping any null Entries.
-
of
public static <K,V> PersistentHashMap<K,V> of(java.lang.Iterable<java.util.Map.Entry<K,V>> kvPairs)
Returns a new PersistentHashMap of the given keys and their paired values. There is also a varargs version of this method:StaticImports.map(Map.Entry...). Use theStaticImports.tup(Object, Object)method to define key/value pairs briefly and easily.- Parameters:
kvPairs- Key/value pairs (to go into the map). In the case of a duplicate key, later values in the input list overwrite the earlier ones. The resulting map can contain zero or one null key and any number of null values. Null k/v pairs will be silently ignored.- Returns:
- a new PersistentHashMap of the given key/value pairs
-
equator
public Equator<K> equator()
Returns the Equator used by this map for equals comparisons and hashCodes- Specified by:
equatorin interfaceBaseUnsortedMap<K,V>
-
assoc
public PersistentHashMap<K,V> assoc(K key, V val)
Description copied from interface:ImMapReturns a new map with the given key/value added
-
mutable
public PersistentHashMap.MutableHashMap<K,V> mutable()
Description copied from interface:ImMapReturns a mutable version of this mutable map.
-
entry
public Option<UnmodMap.UnEntry<K,V>> entry(K key)
Description copied from interface:BaseMapReturns an option of the key/value pair associated with this key
-
iterator
public UnmodIterator<UnmodMap.UnEntry<K,V>> iterator()
Description copied from interface:UnmodIterableA one-time use, mutable, not-thread-safe way to get each value of the underling collection in turn. I experimented with various thread-safe alternatives, but the JVM is optimized around iterators so this is the lowest common denominator of collection iteration, even though iterators are inherently mutable.- Specified by:
iteratorin interfacejava.lang.Iterable<K>- Specified by:
iteratorin interfaceUnmodIterable<K>
-
keyIterator
public UnmodIterator<K> keyIterator()
- Specified by:
keyIteratorin interfaceUnmodMap<K,V>
-
valIterator
public UnmodIterator<V> valIterator()
- Specified by:
valIteratorin interfaceUnmodMap<K,V>
-
size
public int size()
Returns the number of items in this collection or iterable.
-
-