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
Post a Comment