com.devexperts.util
Class LongHashMap<V>

java.lang.Object
  extended by java.util.AbstractMap<Long,V>
      extended by com.devexperts.util.AbstractLongMap<V>
          extended by com.devexperts.util.LongHashMap<V>
All Implemented Interfaces:
LongMap<V>, Serializable, Cloneable, Map<Long,V>

public final class LongHashMap<V>
extends AbstractLongMap<V>
implements Cloneable, Serializable

Hashtable based implementation of the LongMap interface. This implementation provides all of the optional map operations, and permits null values. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Note that this implementation is not synchronized. If multiple threads access this map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap(java.util.Map) method. This is best done at creation time, to prevent accidental unsynchronized access to the map.

The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface com.devexperts.util.LongMap
LongMap.Entry<V>
 
Field Summary
static int MAX_CAPACITY
          Maximal supported capacity of this hashtable.
 
Constructor Summary
LongHashMap()
          Constructs a new, empty map.
LongHashMap(int capacity)
          Constructs a new map with a specified capacity.
LongHashMap(Map<? extends Long,? extends V> m)
          Constructs a new map containing all mappings from the specified map.
 
Method Summary
 void clear()
           
 void clearAndCompact()
          Removes all mappings and frees all memory.
 void clearAndCompact(int capacity)
          Removes all mappings and compacts memory usage of this hashtable, but requested capacity is retained.
 Object clone()
          Returns a shallow copy of this LongHashMap instance: the values themselves are not cloned.
 void compact()
          Compacts memory usage of this hashtable.
 void compact(int capacity)
          Compacts memory usage of this hashtable, but requested capacity is retained.
 boolean containsKey(long key)
          Returns true if this map contains a mapping for the specified key.
 void ensureCapacity(int capacity)
          Makes sure that no rehashes or memory reallocations will be needed until size() <= capacity.
 Set<Map.Entry<Long,V>> entrySet()
          Returns a collection view of the mappings contained in this map.
 V get(long key)
          Returns the value to which this map maps the specified key.
 boolean isEmpty()
           
 LongSet longKeySet()
          Returns a set view of the keys contained in this map.
 V put(long key, V value)
          Associates the specified value with the specified key in this map.
 void putAll(Map<? extends Long,? extends V> t)
           
 V remove(long key)
          Removes the mapping for this key from this map if present.
 int size()
           
 Collection<V> values()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class com.devexperts.util.AbstractLongMap
containsKey, get, keySet, put, remove
 
Methods inherited from class java.util.AbstractMap
containsValue, equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsValue, equals, hashCode
 

Field Detail

MAX_CAPACITY

public static final int MAX_CAPACITY
Maximal supported capacity of this hashtable.

See Also:
Constant Field Values
Constructor Detail

LongHashMap

public LongHashMap()
Constructs a new, empty map.


LongHashMap

public LongHashMap(Map<? extends Long,? extends V> m)
Constructs a new map containing all mappings from the specified map.


LongHashMap

public LongHashMap(int capacity)
Constructs a new map with a specified capacity.

Method Detail

size

public int size()
Specified by:
size in interface Map<Long,V>
Specified by:
size in class AbstractLongMap<V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<Long,V>
Overrides:
isEmpty in class AbstractMap<Long,V>

clear

public void clear()
Specified by:
clear in interface Map<Long,V>
Specified by:
clear in class AbstractLongMap<V>

putAll

public void putAll(Map<? extends Long,? extends V> t)
Specified by:
putAll in interface Map<Long,V>
Overrides:
putAll in class AbstractMap<Long,V>

containsKey

public boolean containsKey(long key)
Description copied from interface: LongMap
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey in interface LongMap<V>
Specified by:
containsKey in class AbstractLongMap<V>
See Also:
Map.containsKey(Object)

get

public V get(long key)
Description copied from interface: LongMap
Returns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key.

Specified by:
get in interface LongMap<V>
Specified by:
get in class AbstractLongMap<V>
See Also:
Map.get(Object)

put

public V put(long key,
             V value)
Description copied from interface: LongMap
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

Specified by:
put in interface LongMap<V>
Specified by:
put in class AbstractLongMap<V>
See Also:
Map.put(Object, Object)

remove

public V remove(long key)
Description copied from interface: LongMap
Removes the mapping for this key from this map if present.

Specified by:
remove in interface LongMap<V>
Specified by:
remove in class AbstractLongMap<V>
See Also:
Map.remove(Object)

longKeySet

public LongSet longKeySet()
Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It also supports the add and addAll operations -- the mappings to null are added for the corresponding keys. This set is actually implemented via LongHashSet.

Specified by:
longKeySet in interface LongMap<V>
Specified by:
longKeySet in class AbstractLongMap<V>
Returns:
a set view of the keys contained in this map.
See Also:
Map.keySet()

values

public Collection<V> values()
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
values in interface Map<Long,V>
Specified by:
values in class AbstractLongMap<V>
Returns:
a collection view of the values contained in this map.

entrySet

public Set<Map.Entry<Long,V>> entrySet()
Returns a collection view of the mappings contained in this map. Each element in the returned collection is a LongMap.Entry. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface Map<Long,V>
Specified by:
entrySet in class AbstractLongMap<V>
Returns:
a collection view of the mappings contained in this map.
See Also:
LongMap.Entry

ensureCapacity

public void ensureCapacity(int capacity)
Makes sure that no rehashes or memory reallocations will be needed until size() <= capacity.

Throws:
IllegalArgumentException - when capacity > MAX_CAPACITY.

compact

public void compact()
Compacts memory usage of this hashtable. The hashtable's capacity is decreased to the minimum needed size.


compact

public void compact(int capacity)
Compacts memory usage of this hashtable, but requested capacity is retained. This method is identical to the sequence of compact() and ensureCapacity(int) calls, but is faster, because it does not produce excessive garbage. When capacity is less or equal than 1, this method is equivalent to compact().

Throws:
IllegalArgumentException - when capacity > MAX_CAPACITY.

clearAndCompact

public void clearAndCompact()
Removes all mappings and frees all memory. This method is identical to the sequence of clear() and compact() calls, but is faster.


clearAndCompact

public void clearAndCompact(int capacity)
Removes all mappings and compacts memory usage of this hashtable, but requested capacity is retained. This method is identical to the sequence of clear() and compact(int) calls, but is faster. When capacity is less or equal than 1, this method is equivalent to clearAndCompact().

Throws:
IllegalArgumentException - when capacity > MAX_CAPACITY.

clone

public Object clone()
Returns a shallow copy of this LongHashMap instance: the values themselves are not cloned.

Overrides:
clone in class AbstractMap<Long,V>


Copyright © 2013 Devexperts. All Rights Reserved.