Java – how to generate a unique int from a unique string?

I have a string object with a unique ID

How to convert a string to a unique int in the simplest / fastest way?

10X.

Edit – OK I already know string Hashcode is possible But it is not recommended anywhere In fact, if no other method is recommended - if I have my object in a collection and I need a hash code, should I use it Should I connect it to another string to make it more successful?

Solution

No, you don't need to have an implementation that returns a unique value, "obviously", obviously most implementations will be broken

All you want to do is put in a good extension, especially for normal values (if any value is more common than others) In addition to the special knowledge of format, it is best to use the hash code of the string itself

Knowing the limitations of your ID format in particular may customize and lead to better performance, although false assumptions are more likely to make things better

Editor: spread in good bits

As mentioned here and other answers, completely unique is impossible, and hash conflict is possible Hash usage method knows this and can handle it, but it will affect performance, so we hope collision is rare

In addition, hashes are usually rehashed, so our 32 bits may eventually be reduced to, for example One is between 0 and 22, and we want to allocate it as well as possible

We also want to balance this, rather than spend too much time calculating our hash, which itself becomes a bottleneck An imperfect balancing act

A typical example of a bad hash method is one of X and Y coordinate pairs, which is:

return X ^ Y;

Although it is good to return 2 ^ 32 possible values out of 4 ^ 32 possible inputs, in the real world, using a set of coordinates (x and y are equal) ({0,0}, {1,1}, {2,2}), these will be hashed to zero, or matching pairs ({2,3} and {3,2}), which will be hashed to the same number We may better serve:

return ((X << 16) | (x >> 16)) ^ Y;

Now, compared with the former, this terrible value is terrible, but it is often better in the real world

Of course, if you're writing a general course (don't know what possible input is), or better understand the purpose at hand, there's a different job For example, if I use date objects, but know that they are only dates (the time is always at midnight) and can only be within a few years, I may prefer to use only days, months and lower age numbers to exceed the standard Although the author of the date can't work on such knowledge, he should try his best to take care of everyone

Therefore, if I know, for example, that a given string always consists of six case insensitive characters in the range of [AZ] or [0-9] (you seem to be so, but your problem is not clear), I can use an algorithm to assign a value of 0 to 35 (36 possible values of each character) to each character, then traverse the string, multiply the current value by 36 each time, and the next character

Assuming that there is good propagation in IDS, this will be the way, especially if I make such a command so that the low number in my hash matches the most frequently changed character in ID (if it can be called in this way), so the surviving re hash to a smaller range

However, due to lack of knowledge of this format, I can't make this call with certainty, and I can make things worse (slower algorithms have little or no impact on Hash quality)

One advantage you have is that since it is an ID in itself, it is assumed that no other unequal objects have the same ID, so there is no need to check other properties This is not always consistent

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>