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:
Serializable
,Iterable<UnmodMap.UnEntry<K,V>>
,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>, 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 class
PersistentHashMap.MutHashMap<K,V>
-
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K extends Object,V extends Object>
-
Nested classes/interfaces inherited from interface org.organicdesign.fp.collections.UnmodIterable
UnmodIterable.UnIterable
-
Nested classes/interfaces inherited from interface org.organicdesign.fp.collections.UnmodMap
UnmodMap.UnEntry<K,V>
-
-
Field Summary
Fields Modifier and Type Field Description static @NotNull PersistentHashMap<Object,Object>
EMPTY
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description @NotNull PersistentHashMap<K,V>
assoc(K key, V val)
Returns a new map with the given key/value addedstatic <K,V>
@NotNull PersistentHashMap<K,V>empty()
static <K,V>
@NotNull PersistentHashMap<K,V>empty(@Nullable Equator<K> e)
static <K,V>
@NotNull PersistentHashMap.MutHashMap<K,V>emptyMutable()
Works around some type inference limitations of Java 8.static <K,V>
@NotNull PersistentHashMap.MutHashMap<K,V>emptyMutable(Equator<K> e)
Works around some type inference limitations of Java 8.@NotNull Option<UnmodMap.UnEntry<K,V>>
entry(K key)
Returns an option of the key/value pair associated with this key@NotNull Equator<K>
equator()
Returns the Equator used by this map for equals comparisons and hashCodes@NotNull UnmodIterator<UnmodMap.UnEntry<K,V>>
iterator()
A one-time use, mutable, not-thread-safe way to get each value of the underling collection in turn.@NotNull UnmodIterator<K>
keyIterator()
@NotNull PersistentHashMap.MutHashMap<K,V>
mutable()
Returns a mutable version of this mutable map.static <K,V>
@NotNull PersistentHashMap<K,V>of(@Nullable Iterable<Map.Entry<K,V>> kvPairs)
Returns a new PersistentHashMap of the given keys and their paired values.static <K,V>
@NotNull PersistentHashMap<K,V>ofEq(Equator<K> eq, @Nullable Iterable<Map.Entry<K,V>> es)
Returns a new PersistentHashMap of the given keys and their paired values, skipping any null Entries.int
size()
Returns the number of items in this collection or iterable.@NotNull UnmodIterator<V>
valIterator()
@NotNull 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 java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface java.util.Map
equals, forEach, getOrDefault, hashCode
-
Methods inherited from interface org.organicdesign.fp.xform.Transformable
any, toImList, toImMap, toImRrbt, toImSet, toImSortedMap, toImSortedSet, toMutList, toMutMap, toMutRrbt, toMutSet, toMutSortedMap, toMutSortedSet
-
Methods inherited from interface org.organicdesign.fp.collections.UnmodIterable
concat, drop, dropWhile, filter, flatMap, fold, foldUntil, head, map, precat, take, takeWhile, whereNonNull
-
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
@NotNull public static final @NotNull PersistentHashMap<Object,Object> EMPTY
-
-
Method Detail
-
empty
@NotNull public static <K,V> @NotNull PersistentHashMap<K,V> empty()
-
emptyMutable
@NotNull public static <K,V> @NotNull PersistentHashMap.MutHashMap<K,V> emptyMutable()
Works around some type inference limitations of Java 8.
-
empty
@NotNull public static <K,V> @NotNull PersistentHashMap<K,V> empty(@Nullable @Nullable Equator<K> e)
-
emptyMutable
@NotNull public static <K,V> @NotNull PersistentHashMap.MutHashMap<K,V> emptyMutable(Equator<K> e)
Works around some type inference limitations of Java 8.
-
ofEq
@NotNull public static <K,V> @NotNull PersistentHashMap<K,V> ofEq(Equator<K> eq, @Nullable @Nullable Iterable<Map.Entry<K,V>> es)
Returns a new PersistentHashMap of the given keys and their paired values, skipping any null Entries.
-
of
@NotNull public static <K,V> @NotNull PersistentHashMap<K,V> of(@Nullable @Nullable Iterable<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
@NotNull public @NotNull Equator<K> equator()
Returns the Equator used by this map for equals comparisons and hashCodes- Specified by:
equator
in interfaceBaseUnsortedMap<K,V>
-
assoc
@NotNull public @NotNull PersistentHashMap<K,V> assoc(K key, V val)
Description copied from interface:ImMap
Returns a new map with the given key/value added
-
mutable
@NotNull public @NotNull PersistentHashMap.MutHashMap<K,V> mutable()
Description copied from interface:ImMap
Returns a mutable version of this mutable map.
-
entry
@NotNull public @NotNull Option<UnmodMap.UnEntry<K,V>> entry(K key)
Description copied from interface:BaseMap
Returns an option of the key/value pair associated with this key
-
iterator
@NotNull public @NotNull UnmodIterator<UnmodMap.UnEntry<K,V>> iterator()
Description copied from interface:UnmodIterable
A 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.
-
keyIterator
@NotNull public @NotNull UnmodIterator<K> keyIterator()
- Specified by:
keyIterator
in interfaceUnmodMap<K,V>
-
valIterator
@NotNull public @NotNull UnmodIterator<V> valIterator()
- Specified by:
valIterator
in interfaceUnmodMap<K,V>
-
size
public int size()
Returns the number of items in this collection or iterable.
-
-