public class SparseHashArray<T> extends Object implements SparseArray<T>, Iterable<ObjectEntry<T>>, Serializable
HashMap
. This class offers amortized
constant time access to array indices at the expense of space.
Instance offer a space savings of retaining only the non-zero indices and
values. For large array with only a few values set, this offers a huge
savings. However, as the cardinality of the array grows in relation to its
size, a dense array will offer better performance in both space and time.
This is especially true if the sparse array instance approaches a cardinality
to size ratio of .5
.
This class offers much better performance than IntegerMap
, but will
use significantly more space as the cardinality increases. In addition, this
class will marshall primitive types into their reified object forms.
SparseArray
,
IntegerMap
,
Serialized FormConstructor and Description |
---|
SparseHashArray()
Creates a sparse array that grows to the maximum size set by
Integer.MAX_VALUE . |
SparseHashArray(int length)
Creates a sparse array with a fixed length
|
SparseHashArray(T[] array)
Creates a sparse array copy of the provided array, retaining only the
non-zero values.
|
Modifier and Type | Method and Description |
---|---|
int |
cardinality()
Returns the number of non-zero values in this sparse array
|
T |
get(int index)
Returns the value of this array at the index.
|
int[] |
getElementIndices()
Returns the indices of the array that contain non-
0 values. |
Iterator<ObjectEntry<T>> |
iterator()
Returns an iterator over the non-
null values in this array. |
int |
length()
Returns the maximum length of this array.
|
void |
set(int index,
T value)
Sets the object as the value at the index.
|
<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.
|
public SparseHashArray()
Integer.MAX_VALUE
.public SparseHashArray(int length)
public SparseHashArray(T[] array)
public int cardinality()
cardinality
in interface SparseArray<T>
public T get(int index)
get
in interface SparseArray<T>
index
- the position in the arraypublic int[] getElementIndices()
0
values.getElementIndices
in interface SparseArray<T>
public Iterator<ObjectEntry<T>> iterator()
null
values in this array. This
method makes no guarantee about the order in which the indices are
returned.iterator
in interface Iterable<ObjectEntry<T>>
public int length()
length
in interface SparseArray<T>
public void set(int index, T value)
set
in interface SparseArray<T>
index
- an index in the arrayvalue
- the valuepublic <E> E[] toArray(E[] array)
toArray
in interface SparseArray<T>
Copyright © 2012. All Rights Reserved.