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

后端 未结 6 1423
生来不讨喜
生来不讨喜 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:03

    You can't get a unique integer from a String of unlimited length. There are 4 billionish (2^32) unique integers, but an almost infinite number of unique strings.

    String.hashCode() will not give you unique integers, but it will do its best to give you differing results based on the input string.

    EDIT

    Your edited question says that String.hashCode() is not recommended. This is not true, it is recommended, unless you have some special reason not to use it. If you do have a special reason, please provide details.

提交回复
热议问题