java - Impact of load factor on lookup time? -


as java doc explains

as general rule, default load factor (.75) offers tradeoff between time , space costs. higher values decrease space overhead increase lookup cost (reflected in of operations of hashmap class, including , put).

i not getting how increasing load factor 1, increase lookup time

for example:- initial capacity 16 , load factor 1 resizing 32 happen after size reaches 16 * 1 = 16. if put new new entry how lookup time more in comparison if load factor have been .75 (in case hashmap have resized @ size 2 )

as answer says what significance of load factor in hashmap? lesser number of free buckets ,higher chances of collision.

i not sure how number of free buckets related chance of collision.

per mine understanding, bucket decided based on hashcode of key object. if comes out same key object in bucket there chances of collision otherwise go different bucket (out of availablke bucket ). how come collision related free buckets ? mean if hashcode different , hashmap full, try fit in existing bucket ?

its not duplicate of what significance of load factor in hashmap?. asking specific point not answered in link

so how come collision related free buckets ? mean if hashcode different , hashmap full, try fit in existing bucket ?

the hash code of key can int value 0 231-1. means value of hashcode() method larger number of buckets. therefore 2 keys of different hash codes may mapped same bucket.

for example, if number of buckets 16, hash codes 1, 17, 33, 49, etc... mapped bucket #1.

if there more buckets, smaller number of unique hash codes can mapped same bucket, there less chance of same bucket holding multiple entries.

for example, if number of buckets increased 32, hash codes 1 & 33 still mapped bucket #1, hash codes 17, 49, etc... mapped different bucket (#17) - hence less chance of collision.

when load factor < 1, average number of entries in each bucket < 1, gives chance of constant lookup time (unless key has terrible hashcode implementation returns few unique values).


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -