java - Are hashCodes unique for Strings? -


recently, came across piece of code, map<integer, string> used, integer(key) hashcode of string , string value corresponding that.

is right thing do? because now, equals not called string when calling get. (get done using hashcode() method on string object.

or, hashcode(s) unique unique strings?

i checked equals od string class. there logic written that. confused.

a hashmap does use equals() compare keys. uses hashcode() find bucket key located, , drastically reduce number of keys compare equals().

obviously, hashcode() can't produce unique values, since int limited 2^32 distinct values, , there infinity of possible string values.

in conclusion, result of hashcode() not fit key of map.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -