how can i generate a unique int from a unique string?

后端 未结 6 1426
生来不讨喜
生来不讨喜 2020-12-24 09:32

I have an object with a String that holds a unique id . (such as \"ocx7gf\" or \"67hfs8\") I need to supply it an implementation of int hascode() which will be unique obvio

6条回答
  •  爱一瞬间的悲伤
    2020-12-24 10:12

    Looks like you've got a base-36 number there (a-z + 0-9). Why not convert it to an int using Integer.parseInt(s, 36)? Obviously, if there are too many unique IDs, it won't fit into an int, but in that case you're out of luck with unique integers and will need to get by using String.hashCode(), which does its best to be close to unique.

提交回复
热议问题