org.javimmutable.collections.hash
Class JImmutableHashMap<T,K,V>

java.lang.Object
  extended by org.javimmutable.collections.common.AbstractJImmutableMap<K,V>
      extended by org.javimmutable.collections.hash.JImmutableHashMap<T,K,V>
All Implemented Interfaces:
Iterable<JImmutableMap.Entry<K,V>>, Cursorable<JImmutableMap.Entry<K,V>>, Insertable<JImmutableMap.Entry<K,V>>, JImmutableMap<K,V>, Mapped<K,V>

@Immutable
public class JImmutableHashMap<T,K,V>
extends AbstractJImmutableMap<K,V>


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.javimmutable.collections.JImmutableMap
JImmutableMap.Entry<K,V>
 
Method Summary
 JImmutableMap<K,V> assign(K key, V value)
          Sets the value associated with a specific key.
 Cursor<JImmutableMap.Entry<K,V>> cursor()
           
 JImmutableMap<K,V> delete(K key)
          Deletes the entry for the specified key (if any).
 JImmutableMap<K,V> deleteAll()
           
 Holder<V> find(K key)
          Search for a value within the map and return a Holder indicating if the value was found and, if it was found, the value itself.
 Holder<JImmutableMap.Entry<K,V>> findEntry(K key)
          Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself.
static
<K,V> JImmutableMap<K,V>
forKey(K key)
          Returns an empty map using the appropriate collision handling strategy for the given key's class.
 V getValueOr(K key, V defaultValue)
          Return the value associated with key or defaultValue if no value is associated.
static
<K,V> EmptyHashMap<K,V>
of()
          Returns an empty hash map.
static
<K,V> JImmutableMap<K,V>
of(Class<K> klass)
          Returns an empty map using the appropriate collision handling strategy for keys of the given class.
 int size()
          Return the number of entries in the map.
static
<K,V> JImmutableMap<K,V>
usingList()
          Returns an empty map using linked lists for handling hash code collisions.
static
<K extends Comparable<K>,V>
JImmutableMap<K,V>
usingTree()
          Returns an empty map using linked lists for handling hash code collisions.
 
Methods inherited from class org.javimmutable.collections.common.AbstractJImmutableMap
equals, get, getMap, hashCode, insert, isEmpty, iterator, keysCursor, toString, valuesCursor
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

of

public static <K,V> EmptyHashMap<K,V> of()
Returns an empty hash map. The empty map will automatically select a collision handling strategy on the first call to assign() based on the key for that call. For this reason all keys used for a given map must either implement or not implement Comparable. If some keys implement it and some do not the collision handling code will likely fail due to a class cast exception or a method not defined exception.

Type Parameters:
K -
V -
Returns:

of

public static <K,V> JImmutableMap<K,V> of(Class<K> klass)
Returns an empty map using the appropriate collision handling strategy for keys of the given class. All keys used with that map should derive from the specified class to avoid runtime problems with incompatible keys.

Type Parameters:
K -
V -
Parameters:
klass -
Returns:

forKey

public static <K,V> JImmutableMap<K,V> forKey(K key)
Returns an empty map using the appropriate collision handling strategy for the given key's class. All keys used with that map should derive from the specified key's class to avoid runtime problems with incompatible keys.

Type Parameters:
K -
V -
Parameters:
key -
Returns:

usingList

public static <K,V> JImmutableMap<K,V> usingList()
Returns an empty map using linked lists for handling hash code collisions. This is safe for any type of key but is slower when many keys have the same hash code.

Type Parameters:
K -
V -
Returns:

usingTree

public static <K extends Comparable<K>,V> JImmutableMap<K,V> usingTree()
Returns an empty map using linked lists for handling hash code collisions. This is faster than the list based collision handling but depends on all keys implementing Comparable and being able to compare themselves to all other keys.

Type Parameters:
K -
V -
Returns:

getValueOr

public V getValueOr(K key,
                    V defaultValue)
Description copied from interface: Mapped
Return the value associated with key or defaultValue if no value is associated. Note that if defaultValue is an acceptable value to the container then this method will be ambiguous and find() should be used instead.

Parameters:
key - identifies the value to retrieve
defaultValue - value to return if no entry exists for key
Returns:
value associated with key or defaultValue if no value is associated

find

@Nonnull
public Holder<V> find(@Nonnull
                              K key)
Description copied from interface: JImmutableMap
Search for a value within the map and return a Holder indicating if the value was found and, if it was found, the value itself. Holder allows null values to be returned unambiguously.

Parameters:
key - non-null key to search for
Returns:
empty Holder if not found, otherwise filled Holder with value

findEntry

@Nonnull
public Holder<JImmutableMap.Entry<K,V>> findEntry(@Nonnull
                                                          K key)
Description copied from interface: JImmutableMap
Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself.

Parameters:
key - non-null key to search for
Returns:
empty Holder if not found, otherwise filled Holder with Entry

assign

@Nonnull
public JImmutableMap<K,V> assign(@Nonnull
                                         K key,
                                         V value)
Description copied from interface: JImmutableMap
Sets the value associated with a specific key. Key must be non-null but value can be null. If the key already has a value in the map the old value is discarded and the new value is stored in its place. Returns a new PersistentMap reflecting any changes. The original map is always left unchanged.

Parameters:
key - non-null key
value - possibly null value
Returns:
new map reflecting the change

delete

@Nonnull
public JImmutableMap<K,V> delete(@Nonnull
                                         K key)
Description copied from interface: JImmutableMap
Deletes the entry for the specified key (if any). Returns a new map if the value was deleted or the current map if the key was not contained in the map.

Parameters:
key - non-null key
Returns:
same or different map depending on whether key was removed

size

public int size()
Description copied from interface: JImmutableMap
Return the number of entries in the map.

Returns:

deleteAll

@Nonnull
public JImmutableMap<K,V> deleteAll()
Returns:
an equivalent collection with no values

cursor

@Nonnull
public Cursor<JImmutableMap.Entry<K,V>> cursor()


Copyright © 2014 Burton Computer Corporation. All rights reserved.