Question

Please tell me what will be with old items in HashMap if I put more items than specify in capacity? for example:

HashMap<String, Bitmap> hashmap= new HashMap<String, Bitmap>(5);

i set capacity to 5. but what will be with first 5 items and bitmap's if I put 10 items to this HashMap?

Was it helpful?

Solution

You're only specifying an initial capacity - the HashMap will grow as it needs to anyway, copying the contents internally. It's only available as an optimization, so that if you know you'll need a large capacity, you can start off with that capacity so that no copying is required.

From the documentation:

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 capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before 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.

...

If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top