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


I have an object with a string that has a unique ID (like "OSX 7 GF" or "67 HFS 8 ") I need an implementation of the INSI code (which is clearly unique).

The easiest / fastest way I can?

10x

Edit - OK I already know that String.hashcode is possible but it is not recommended in any place. In fact, 'if any other method is not recommended - should I use it or not, if my object is in the archive and I need to have a shake, do I include it in another string to make it more successful? needed?

No, you need to implement not the unique value, " Clearly ", as most implementations will break.

What would you like to do, especially if the general values ​​(if a value is more common than others), except for the special knowledge of its form, then it would be best to use the string of the string.

With special knowledge of the boundaries of your ID format, optimizing it and performing better, though the misconceptions are more likely to improve the better things.

Edit: On the good spread of bits.

As mentioned here and in the other answer, it is impossible to be completely unique and hash conflicts are possible, it is possible to know how to use the hash-experimenting methods and to deal with it, but on display does effect, so we want the collision to be rare.

In addition, the hash is usually washed again, our 32-bit number can be exhausted, eg a boundary ranges from 0 to 22, and we want as good distribution as possible.

We also want to make a balance with not counting our Hush for so long, it becomes an obstacle itself. An imperfect balance work.

An excellent example of bad hash method is one for a coordinate pair of x, y intus:

  back to x ^ Y; Although it works perfectly well to return 2 ^ 32 potential values ​​from 2 ^ 32 potential values, its usage in the real world is quite normal for a set of coordinates. Where x and y are equal ({0, 0}, {1, 1}, {2, 2} and so on) that add zero or matching all the hash ({2,3} and {3, 2}) That's the same number we possibly serve better than:  
  return ((x and lt; 16) | (x> gt; 16)) ^ Y;   

Now, there is a lot of possible value as bus, for which it is terrible for the past, but it gives better service in real world affairs . / P>

Of course, if you have a general purpose class (no probable ideas, no information) or a better idea of ​​purpose in hand, then there is a different job. For example, if I was using date objects, but knew that they would be all the dates (time portion is always midnight) and only in a few years of each other, then I would prefer custom hash codes Those who can only work on such knowledge of day, month and year, lower than the standard one, even though date can try to fulfill for everyone .

Therefore, if I knew for example that a given string always contains 6 case-insensitive characters in range [AJ] or [0- 9] (which you seem to be, but with your question It is not clear that it does this) then I can use an algorithm which is 0 for every character 35 (36 possible values ​​of each character), and then walking through the string, multiplying the current value by 36 every time And add value to the next four Received.

In a good dissemination ID, this will be the way to go, especially if I have ordered that the lower-important digits of my hashes match the most frequently changed fours in the ID (if such a call Can be done), so living

However, there is definitely a lack of such knowledge, I definitely can not call it, and I can ruin things well. (Hash quality or even slightly Slow algorithm for positive benefits).

You have an advantage that since it is an ID itself, then there is probably no other non-identical object identical ID, and therefore no other property is needed to check it always Does not catch

Comments