Bad idea to use String key in HashMap?

前端 未结 5 434
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 10:43

I understand that the String class\' hashCode() method is not guarantied to generate unique hash codes for distinct String-s. I see a lot of usage of putting Strin

5条回答
  •  忘掉有多难
    2020-12-12 11:02

    You are talking about hash collisions. Hash collisions are an issue regardless of the type being hashCode'd. All classes that use hashCode (e.g. HashMap) handle hash collisions just fine. For example, HashMap can store multiple objects per bucket.

    Don't worry about it unless you are calling hashCode yourself. Hash collisions, though rare, don't break anything.

提交回复
热议问题