Hashmap creation performance when I know a finite number of items

If I know the final size of the elements in the HashMap, what's the best way to plot it in terms of performance? Based on JavaDoc, to avoid re-writing, do the following:

int TOTAL_ELEMENTS_TO_BE_STORED = 10;
... = new HashMap<T, Q>( TOTAL_ELEMENTS_TO_BE_STORED + 1, 1.0f );

      

but also:

... = new HashMap<T, Q>( Math.ceil(TOTAL_ELEMENTS_TO_BE_STORED * 1.333) + 1 );

      

I read from the javadoc HashMap:

Higher values ​​reduce the overhead but increase the lookup cost (reflected in most HashMap class operations, including get and put).

Is it true that search costs will be higher? In such a case, it is generally recommended to use the default load factor of 0.75, but give more capacity instead, or vice versa?

+3


source to share


3 answers


Yes, search costs will be higher.

The choice depends on your requirements.



  • You need to find search items quickly and your data is small enough - leave the load factor at 0.75
  • You have a lot of data and don't want to save a lot of memory - use 1.0 as a load factor.

BTW, the load factors are not in the range [0.75, 1] ​​- you can choose any positive value. The more value, the less memory you need, but the larger your search will be.

+1


source


If the question is about performance, and you know the number of elements in advance, it is better to choose a hash table with open addressing (self-recording or from some library), but not standard HashMap

.



With a small number of elements, downtime ArrayList

can be faster than any hash table data structure. You need to do some tests.

+1


source


The most expensive is the resizing

internal array phase , which is used for storage. The entries at this point must be re-deleted and potentially moved to different buckets. Although re-calibration can occur for other reasons; avoiding the obvious, this is a good option.

If you know how many records you will have, just add 33% to that number and leave the default load_factor

0.75

.

For example, if you have 16 buckets, you can only fit 12 records before resizing.

Also the size of the array is as follows power of two

- even if you didn't provide it as such. Therefore, if you have 100 records; 125 - +33%

; whereas the inner size will be 128

.

+1


source







All Articles