(There are some questions about time-efficient sparse arrays but I am looking for memory efficiency.)
I need the equivalent of a List<T>
or Map<Integer,T>
which
ArrayList<T>
in the case that most of the indices are not null
, i.e. when the actual data is not very sparse.null
indices.HashMap<Integer,T>
(as this autoboxes the keys and probably does not take advantage of the scalar key type).Does anyone know of such a utility class?
I would have expected Commons Collections to have one but it did not seem to.
I came across org.apache.commons.math.util.OpenIntToFieldHashMap
which looks almost right except the value type is a FieldElement
which seems gratuitous; I just want T extends Object
. It looks like it would be easy to edit its source code to be more generic, though I would rather use a binary dependency if one is available.
I would look at Android's SparseArray implementation for inspiration. You can view the source by downloading AOSP's source code here http://source.android.com/source/downloading.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With