java - What Exactly is Hash Collision -


hash collision or hashing collision in hashmap not new topic , i've come across several blogs , discussion boards explaining how produce hash collision or how avoid in ambiguous , detailed way. came across question in interview. had lot of things explain think hard precisely give right explanation. sorry if questions repeated here, please route me precise answer:

  1. what hash collision - feature, or common phenomenon mistakenly done avoid?
  2. what causes hash collision - bad definition of custom class' hashcode() method, or leave equals() method un-overridden while imperfectly overriding hashcode() method alone, or not developers , many popular java libraries has classes can cause hash collision?
  3. does go wrong or unexpected when hash collision happens? mean there reason why should avoid hash collision?
  4. does java generate or @ least try generate unique hashcode per class during object initiation? if no, right rely on java alone ensure program not run hash collision jre classes? if not right, how avoid hash collision hashmaps final classes string key?

i'll greateful if please share answers 1 or of these questions.

what hash collision - feature, or common phenomenon mistakenly done avoid?

it's feature. arises out of nature of hashcode: mapping large value space smaller value space. there going collisions, design , intent.

what causes hash collision - bad definition of custom class' hashcode() method,

a bad design can make worse, endemic in notion.

or leave equals() method un-overridden while imperfectly overriding hashcode() method alone,

no.

or not developers , many popular java libraries has classes can cause hash collision?

this doesn't make sense. hashes bound collide sooner or later, , poor algorithms can make sooner. that's it.

does go wrong or unexpected when hash collision happens?

not if hash table competently written. hash collision means hashcode not unique, puts calling equals(), , more duplicates there worse performance.

i mean there reason why should avoid hash collision?

you have trade off ease of computation against spread of values. there no single black , white answer.

does java generate or atleast try generate unique hascode per class during object initiation?

no. 'unique hash code' contradiction in terms.

if no, right rely on java alone ensure program not run hash collision jre classes? if not right, how avoid hash collision hashmaps final classes string key?

the question meaningless. if you're using string don't have choice hashing algorithm, , using class hashcode has been slaved on experts twenty or more years.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -