public class IntegerMap<V> extends AbstractMap<Integer,V> implements SparseArray<V>
When used as a SparseArray
this class allows negative valued indices,
as well as the index of Integer.MAX_VALUE
. This affects the behavior
of two methods. First size
will return Integer.MAX_VALUE
,
but the true maximum size is 232, which is larger than an int
can represent. Second, the toArray
method will only return
those elements that fall within the range of the provided array. This
limitation entails that the values at negative-valued indices will not
be stored. In this case it is best to use the entrySet()
method to
access all of the indices and associated values in order.
This class makes a trade off for reduced space usage at the cost of decreased
performace. The put
, get
, containsKey
and get
operations are all logarithmic when the map is unmodified. If a new
mapping is added, or one is removed, the operation is linear in the number of
mappings. Both size
and isEmpty
are still constant time.
The get
operation runs in logarithmic time. The set
operation runs in consant time if setting an existing non-zero value to a
non-zero value. However, if the set
invocation sets a zero value to
non-zero, the operation is linear with the size of the array.
This map does not allow null
keys, but does allow null
values
.
Implementation Note: the Iterator.remove()
method is currently
unsupported and will throw an exception when called. However, a future
implementation will fix this.
TrieMap
,
Map
,
SparseArray
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
IntegerMap()
Creates a new map.
|
IntegerMap(Map<Integer,? extends V> m)
Creates a new map with the mappings contained in
m . |
IntegerMap(V[] array)
Creates a sparse copy of the provided object array, retaining only those
non-
null values and their associated indices. |
Modifier and Type | Method and Description |
---|---|
int |
cardinality()
Returns the number of non-zero values in this sparse array
|
void |
clear()
Removes all of the mappings from this map.
|
boolean |
containsKey(Object key)
Returns
true if this map contains a mapping for the specified key. |
boolean |
containsValue(Object value) |
Set<Map.Entry<Integer,V>> |
entrySet()
Returns a
Set view of the mappings contained in this map. |
V |
get(int index)
Returns the value of this array at the index.
|
V |
get(Object key)
Returns the value to which the specified key is mapped, or
null
if this map contains no mapping for the key. |
int[] |
getElementIndices()
Returns the indices of the array that contain non-
null values. |
Set<Integer> |
keySet()
Returns a
Set view of the keys contained in this map. |
int |
length()
Returns length of this array.
|
V |
put(Integer key,
V value)
Adds the mapping from the provided key to the value.
|
V |
remove(Object key)
Removes the mapping for a key from this map if it is present and returns
the value to which this map previously associated the key, or
null if the map contained no mapping for the key. |
void |
set(int index,
V obj)
Sets the object as the value at the index.
|
int |
size()
Returns the number of key-value mappings in this trie.
|
<E> E[] |
toArray(E[] array)
Fills the provided array with the values contained in this array that fit
within the length of the provided array.
|
Collection<V> |
values()
Returns a
Collection view of the values contained in this map. |
public IntegerMap()
public IntegerMap(Map<Integer,? extends V> m)
m
.public IntegerMap(V[] array)
null
values and their associated indices.public int cardinality()
cardinality
in interface SparseArray<V>
public void clear()
public boolean containsKey(Object key)
true
if this map contains a mapping for the specified key.containsKey
in interface Map<Integer,V>
containsKey
in class AbstractMap<Integer,V>
public boolean containsValue(Object value)
containsValue
in interface Map<Integer,V>
containsValue
in class AbstractMap<Integer,V>
public Set<Map.Entry<Integer,V>> entrySet()
Set
view of the mappings contained in this map.public V get(Object key)
null
if this map contains no mapping for the key.public V get(int index)
get
in interface SparseArray<V>
index
- the position in the arraypublic int[] getElementIndices()
null
values.getElementIndices
in interface SparseArray<V>
public int length()
SparseArray
length
in interface SparseArray<V>
public V put(Integer key, V value)
put
in interface Map<Integer,V>
put
in class AbstractMap<Integer,V>
key
- value
- NullPointerException
- if the key is null
IllegalArgumentException
- if the key is not an instance of Integer
public V remove(Object key)
null
if the map contained no mapping for the key.public void set(int index, V obj)
set
in interface SparseArray<V>
index
- an index in the arrayobj
- the valuepublic int size()
public <E> E[] toArray(E[] array)
toArray
in interface SparseArray<V>
public Collection<V> values()
Collection
view of the values contained in this map.Copyright © 2012. All Rights Reserved.