com.devexperts.util
Class IndexedMap<K,V>

java.lang.Object
  extended by com.devexperts.util.IndexedMap<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>

public class IndexedMap<K,V>
extends Object
implements Map<K,V>, Cloneable, Serializable

A wrapper around IndexedSet which implements Map interface. Note that in order to operate normally the IndexedMap shall be used with an Indexer which supports explicit object keys. See IndexedSet for more implementation details.

The IndexedMap does not support null values, but it supports null keys if they are supported by corresponding Indexer. The IndexedMap can be serialized if all elements and the Indexer are serializable.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
IndexedMap()
          Creates new empty map with default indexer Indexer.DEFAULT and default initial capacity.
IndexedMap(Collection<V> c)
          Creates a new map containing the elements in the specified collection.
IndexedMap(IndexedSet<K,V> set, boolean wrap)
          Creates a new map which wraps specified indexed set and provides a Map view for it.
IndexedMap(Indexer<K,? super V> indexer)
          Creates new empty map with specified indexer and default initial capacity.
IndexedMap(Indexer<K,? super V> indexer, Collection<? extends V> c)
          Creates a new map with specified indexer containing the elements in the specified collection.
IndexedMap(Indexer<K,? super V> indexer, int initial_capacity)
          Creates new empty map with specified indexer and specified initial capacity.
IndexedMap(Indexer<K,? super V> indexer, Map<? extends K,? extends V> map)
          Creates a new map with specified indexer containing the elements in the specified map.
IndexedMap(int initial_capacity)
          Creates new empty map with default indexer Indexer.DEFAULT and specified initial capacity.
IndexedMap(Map<K,V> map)
          Creates a new map containing the elements in the specified map.
 
Method Summary
 void clear()
          Removes all elements from this map.
 IndexedMap<K,V> clone()
          Returns a shallow copy of this map - the keys and values themselves are not cloned.
 boolean containsKey(long key)
          Returns true if this map contains element which matches specified key.
 boolean containsKey(Object key)
          Returns true if this map contains element which matches specified key.
 boolean containsValue(Object value)
          Returns true if this map contains element which matches specified value.
 void ensureCapacity(int capacity)
          Increases the capacity of this map instance, if necessary, to ensure that it can hold at least the number of elements specified by the capacity argument.
 Set<Map.Entry<K,V>> entrySet()
          Returns a set view of the mapping contained in this map.
 boolean equals(Object o)
          Compares the specified object with this map for equality.
 V get(Object key)
          Deprecated. Use getByKey(K) to be explicit about type and intent.
 V getByKey(K key)
          Returns the element from this map which matches specified key or null if none were found.
 V getByKey(long key)
          Returns the element from this map which matches specified key or null if none were found.
 V getByValue(V value)
          Returns the element from this map which matches specified value or null if none were found.
 IndexedSet<K,V> getIndexedSet()
          Returns indexed set used by this map for actual data storage.
 Indexer<K,? super V> getIndexer()
          Returns indexer used to distinguish and identify elements in this map.
 int hashCode()
          Returns the hash code value for this map.
 boolean isEmpty()
          Tests if this map has no elements.
 Set<K> keySet()
          Returns a set view of the keys contained in this map.
 V put(K key, V value)
          Puts specified element into this map and returns previous element that matches specified one.
 V put(V value)
          Puts specified element into this map and returns previous element that matches specified one.
 void putAll(Collection<? extends V> c)
          Puts all of the elements in the specified collection into this map.
 void putAll(Map<? extends K,? extends V> map)
          Puts all of the elements in the specified map into this map.
 V remove(Object key)
          Deprecated. Use removeKey(K) to be explicit about type and intent.
 V removeKey(K key)
          Removes the element from this map which matches specified key if it is present and returns removed element or null if none were found.
 V removeKey(long key)
          Removes the element from this map which matches specified key if it is present and returns removed element or null if none were found.
 V removeValue(V value)
          Removes the element from this map which matches specified value if it is present and returns removed element or null if none were found.
 int size()
          Returns the number of elements in this map.
 String toString()
          Returns a string representation of this map.
 void trimToSize()
          Trims the capacity of this map instance to be the map's current size.
 Collection<V> values()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IndexedMap

public IndexedMap()
Creates new empty map with default indexer Indexer.DEFAULT and default initial capacity.


IndexedMap

public IndexedMap(int initial_capacity)
Creates new empty map with default indexer Indexer.DEFAULT and specified initial capacity.


IndexedMap

public IndexedMap(Indexer<K,? super V> indexer)
Creates new empty map with specified indexer and default initial capacity.


IndexedMap

public IndexedMap(Indexer<K,? super V> indexer,
                  int initial_capacity)
Creates new empty map with specified indexer and specified initial capacity.


IndexedMap

public IndexedMap(Collection<V> c)
Creates a new map containing the elements in the specified collection. If specified collection is an IndexedSet, then new indexed map uses same indexer, otherwise it uses default indexer Indexer.DEFAULT.


IndexedMap

public IndexedMap(Indexer<K,? super V> indexer,
                  Collection<? extends V> c)
Creates a new map with specified indexer containing the elements in the specified collection.


IndexedMap

public IndexedMap(Map<K,V> map)
Creates a new map containing the elements in the specified map. If specified collection is an IndexedMap, then new indexed map uses same indexer, otherwise it uses default indexer Indexer.DEFAULT.


IndexedMap

public IndexedMap(Indexer<K,? super V> indexer,
                  Map<? extends K,? extends V> map)
Creates a new map with specified indexer containing the elements in the specified map.


IndexedMap

public IndexedMap(IndexedSet<K,V> set,
                  boolean wrap)
Creates a new map which wraps specified indexed set and provides a Map view for it. The wrapping works only if wrap parameter is true. If wrap parameter is false then new independent map is created containing the elements in the specified set - see IndexedMap(Collection) constructor.

Method Detail

clone

public IndexedMap<K,V> clone()
Returns a shallow copy of this map - the keys and values themselves are not cloned.

Overrides:
clone in class Object

ensureCapacity

public void ensureCapacity(int capacity)
Increases the capacity of this map instance, if necessary, to ensure that it can hold at least the number of elements specified by the capacity argument.


trimToSize

public void trimToSize()
Trims the capacity of this map instance to be the map's current size. An application can use this operation to minimize the storage of this map instance.


clear

public void clear()
Removes all elements from this map.

Specified by:
clear in interface Map<K,V>

getIndexer

public Indexer<K,? super V> getIndexer()
Returns indexer used to distinguish and identify elements in this map.


getIndexedSet

public IndexedSet<K,V> getIndexedSet()
Returns indexed set used by this map for actual data storage.


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 view supports all operations.

Specified by:
values in interface Map<K,V>

keySet

public Set<K> keySet()
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 view supports all operations except operations which add new elements.

Specified by:
keySet in interface Map<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()
Returns a set view of the mapping contained in this map. Each element in the returned set is a Map.Entry. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The view supports all operations.

Specified by:
entrySet in interface Map<K,V>

size

public int size()
Returns the number of elements in this map.

Specified by:
size in interface Map<K,V>

isEmpty

public boolean isEmpty()
Tests if this map has no elements.

Specified by:
isEmpty in interface Map<K,V>

get

public V get(Object key)
Deprecated. Use getByKey(K) to be explicit about type and intent.

Returns the element from this map which matches specified key or null if none were found.

Note, that unlike HashMap.get(java.lang.Object), this method might throw ClassCastException if key is of the wrong class.

Specified by:
get in interface Map<K,V>

getByValue

public V getByValue(V value)
Returns the element from this map which matches specified value or null if none were found.


getByKey

public V getByKey(K key)
Returns the element from this map which matches specified key or null if none were found.


getByKey

public V getByKey(long key)
Returns the element from this map which matches specified key or null if none were found.


containsValue

public boolean containsValue(Object value)
Returns true if this map contains element which matches specified value.

Note, that unlike HashMap.containsValue(java.lang.Object), this method might throw ClassCastException if value is of the wrong class.

Specified by:
containsValue in interface Map<K,V>

containsKey

public boolean containsKey(Object key)
Returns true if this map contains element which matches specified key.

Note, that unlike HashMap.containsKey(java.lang.Object), this method might throw ClassCastException if key is of the wrong class.

Specified by:
containsKey in interface Map<K,V>

containsKey

public boolean containsKey(long key)
Returns true if this map contains element which matches specified key.


put

public V put(V value)
Puts specified element into this map and returns previous element that matches specified one.


put

public V put(K key,
             V value)
Puts specified element into this map and returns previous element that matches specified one.

Specified by:
put in interface Map<K,V>
Throws:
IllegalArgumentException - if specified key does not match specified value.

remove

public V remove(Object key)
Deprecated. Use removeKey(K) to be explicit about type and intent.

Removes the element from this map which matches specified key if it is present and returns removed element or null if none were found.

Note, that unlike HashMap.remove(java.lang.Object), this method might throw ClassCastException if key is of the wrong class.

Specified by:
remove in interface Map<K,V>

removeValue

public V removeValue(V value)
Removes the element from this map which matches specified value if it is present and returns removed element or null if none were found.


removeKey

public V removeKey(K key)
Removes the element from this map which matches specified key if it is present and returns removed element or null if none were found.


removeKey

public V removeKey(long key)
Removes the element from this map which matches specified key if it is present and returns removed element or null if none were found.


putAll

public void putAll(Collection<? extends V> c)
Puts all of the elements in the specified collection into this map.


putAll

public void putAll(Map<? extends K,? extends V> map)
Puts all of the elements in the specified map into this map.

Specified by:
putAll in interface Map<K,V>
Throws:
IllegalArgumentException - if specified keys do not match specified values.

equals

public boolean equals(Object o)
Compares the specified object with this map for equality. Obeys the general contract of the Map.equals(Object) method.

Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object

hashCode

public int hashCode()
Returns the hash code value for this map. Obeys the general contract of the Map.hashCode() method.

Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class Object

toString

public String toString()
Returns a string representation of this map.

Overrides:
toString in class Object


Copyright © 2013 Devexperts. All Rights Reserved.