Most likely HashMap
, whose implementation is the simplest among the alternatives available: a simple spreadsheet non-synchronized, non-concurrent, unordered (in the sense that there is no guarantee in the order that its iterator returns elements) and non-navigable (in the sense of NavigableMap
).
The documentation suggests an optimization when instantiating HashMap
:
This implementation provides constant-time performance for the basic
operations (get and put), assuming the hash function disperses the
elements properly among the buckets. Iteration over collection views
requires time proportional to the "capacity" of the HashMap instance
(the number of buckets) plus its size (the number of key-values
mappings). Thus, it is very important not to set the initial capacity
too high (or the load factor too low) if iteration performance is
important.
An instance of HashMap has two parameters that affect its performance:
initial capacity and load factor. The capacity is the number of
buckets in the hash table, and the initial capacity is simply the
the hash table is created. The load factor is a
measure of how full the hash table is allowed to get its
capacity is automatically increased. When the number of entries in the
hash table exceeds the product of the load factor and the current
capacity, the hash table is rehashed (that is, internal data
structures are rebuilt) so that the hash table has approximately twice
the number of buckets.
As a general rule, the default load factor (.75) offers a good
tradeoff between time and space costs. Higher values decrease the
space overhead but increase the lookup cost (reflected in most of the
operations of the HashMap class, including get and put). The expected
number of entries in the map and its load factor should be taken into
account when setting its initial capacity, so as to minimize the
number of rehash operations. If the initial capacity is greater than
the maximum number of entries divided by the load factor, no rehash
operations will ever occur.
The idea is that, if memory is available, avoid costly rehash processes by adopting an initial capacity greater than the quotient between the expected number of inputs and the load factor . In your case that is 2 to 10 thousand elements, so it would be good to instantiate the HashMap
with an initial capacity in the order of 2667 to 13334, since the standard and recommended load factor is 0.75.
Note that if it is in Android , it is worth knowing the android.util.SparseArray
class, which is a more efficient implementation than a HashMap
for use with integer keys.