Provided you have a HashMap
, and you know the maximum number of elements it will hold (at most; for example because you use an enum as key, HashMap<EnumType, Integer> countPerEnumType;
, and you thus know the maximum possible number of elements will be EnumType.values().length
), would you specify initial capacity and load factor for that HashMap? Why (not)?
If one would, I assume the load factor could be 1
? (Safely? Or with adverse side effects?)
Would the initial capacity be length
, length/loadFactor
, or (length/loadFactor) + 1
(i.e. will the HashMap be expanded when it is full, or when it is full and the next element is about to be inserted?
How would you have to set up the hash map to prevent resizing and wasting memory for empty buckets?
EDIT: The example uses Enums, and Lew Bloch suggested EnumMap (beforte I rephrased the question) - but suppose the keys weren't enums, but you still knew the number of elements you'll end up with in advance?