<p>I am looking for a way to create an int\long representation of an arbitrary alpha-numeric String. Hash codes won't do it, because I can't afford hash collisions i.e. the representation must be unique and repeatable. </p>

<p>The numeric representation will be used to perform efficient (hopefully) compares. The creation of the numeric key will take some time, but it only has to happen once, whereas I need to perform vast numbers of comparisons with it - which will hopefully be much faster than comparing the raw Strings.</p>

<p>Any other idea's on faster String comparison will be most appreciated too...</p>

[{'answer_id': 46178, 'author': 'Chris Upchurch', 'author_id': 2600, 'author_profile': 'https://Stackoverflow.com/users/2600', 'pm_score': 0, 'selected': False, 'text': "<p>How long are your strings?  Unless you choose an int representation that's longer than the string, collisions will always be possible no matter what conversion you're using.  So if you're using a 32 bit integer, you can only uniquely represent strings of up to 4 bytes.</p>\n"}, {'answer_id': 46182, 'author': 'Patrick McElhaney', 'author_id': 437, 'author_profile': 'https://Stackoverflow.com/users/437', 'pm_score': 3, 'selected': False, 'text': "<p>Can't you just start with a hash code, and if the hash codes match, do a character by character comparison?</p>\n"}, {'answer_id': 46183, 'author': 'Pramod', 'author_id': 1386292, 'author_profile': 'https://Stackoverflow.com/users/1386292', 'pm_score': 0, 'selected': False, 'text': '<p>How big are your strings? Arbitrarily long strings cannot be compressed into 32/64 bit format. </p>\n'}, {'answer_id': 46188, 'author': 'Thomas Owens', 'author_id': 572, 'author_profile': 'https://Stackoverflow.com/users/572', 'pm_score': 0, 'selected': False, 'text': "<p>If you don't want collisions, try something insane like SHA-512. I can't guarantee there won't be collisions, but I don't think they have found any yet.</p>\n"}, {'answer_id': 46199, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 0, 'selected': False, 'text': '<p>Assuming "alphanumeric" means letters and numbers, you could treat each letter/number as a base-36 digit. Unfortunately, large strings will cause the number to grow rapidly and you\'d have to resort to big integers, which are hardly efficient.</p>\n\n<p>If your strings are usually different when you make the comparison (i.e. searching for a specific string) the hash might be your best option. Once you get a potential hit, you can do the string comparison to be sure. A well-designed hash will make collisions exceedingly rare.</p>\n'}, {'answer_id': 46204, 'author': 'Todd', 'author_id': 3803, 'author_profile': 'https://Stackoverflow.com/users/3803', 'pm_score': 0, 'selected': False, 'text': '<p>It would seem that an <a href="http://en.wikipedia.org/wiki/MD5" rel="nofollow noreferrer">MD5</a> hash would work fine.  The risk of a hash collision would be extremely unlikely.  Depending on the length of your string, a hash that generates an int/long would run into max value problems very quickly. </p>\n'}, {'answer_id': 46209, 'author': 'Matt Warren', 'author_id': 4500, 'author_profile': 'https://Stackoverflow.com/users/4500', 'pm_score': 0, 'selected': False, 'text': "<p>Why don't you do something like 1stChar + (10 x 2ndChar) + 100 x (3rdChar) ...., where you use the simple integer value of each character, i.e. a = 1, b = 2 etc, or just the integer value if it's not a letter. This will give a unique value for each string, even for 2 strings that are just the same letters in a different order.</p>\n\n<p>Of course if gets more complicated if you need to worry about Unicode rather than just ASCII and the numbers could get large if you need to use long string.</p>\n\n<p>Are the standard Java string comparison functions definitely not efficient enough?</p>\n"}, {'answer_id': 46210, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': '<p>How long are the strings? If they are very short, then a unique ID can be generated by considering the characters as digits in base 36 (26 + 10) that form a <em>n</em>-digits number where <em>n</em> is the length of the string. On the other hand, if the strings are short enough to allow this, direct comparison won\'t be an issue anyway.</p>\n\n<p>Otherwise you\'ll have to generate a collision-free hash and this can only be done when the complete problem space is known in advance (i.e. if you know all strings that can possibly occur). You will want to have a look at <a href="http://en.wikipedia.org/wiki/Perfect_hashing" rel="noreferrer">perfect hashing</a>, although the only feasible algorithm to find a perfect hash function that I know is probabilistic so collisions are still theoretically possible.</p>\n\n<p>There might be other ways to find such a function. Knuth called this a “rather amusing …\xa0puzzle” in TAoCP but he doesn\'t give an algorithm either.</p>\n\n<p>In general, you give way too few information to find an algorithm that doesn\'t require probing the whole problem space in some manner. This does invariably mean that the problem has exponential running time but could be solved using machine-learning heuristics. I\'m not sure if this is advisable in your case.</p>\n'}, {'answer_id': 46211, 'author': 'toolkit', 'author_id': 3295, 'author_profile': 'https://Stackoverflow.com/users/3295', 'pm_score': 2, 'selected': False, 'text': '<p>Perhaps:</p>\n\n<pre><code>String y = "oiu291981u39u192u3198u389u28u389u";\nBigInteger bi = new BigInteger(y, 36);\nSystem.out.println(bi);\n</code></pre>\n'}, {'answer_id': 46215, 'author': 'Grzegorz Gierlik', 'author_id': 1483, 'author_profile': 'https://Stackoverflow.com/users/1483', 'pm_score': 1, 'selected': False, 'text': "<p>A few questions in the beginning:</p>\n\n<ol>\n<li>Did you test that simple string comparison is too slow? </li>\n<li>How the comparison looks like ('ABC' == 'abc' or 'ABC' != 'abc')?  </li>\n<li>How many string do you have to compare? </li>\n<li>How many comparison do you have to do?</li>\n<li>How your strings look like (the length, letter case)?</li>\n</ol>\n\n<p>As far as I remember String in Java is an object and two identical strings point to the same object.</p>\n\n<p>So, maybe it would be enough to compare objects (probably string comparison is already implemented in this way).</p>\n\n<p>If it doesn't help you can try to use Pascal implementation of string object when first element is length and if your strings have various length this should save some CPU time.</p>\n"}, {'answer_id': 46216, 'author': 'Don Kirkby', 'author_id': 4794, 'author_profile': 'https://Stackoverflow.com/users/4794', 'pm_score': 4, 'selected': False, 'text': "<p>Unless your string is limited in length, you can't avoid collisions. </p>\n\n<p>There are 4294967296 possible values for an integer (2^32). If you have a string of more than 4 ASCII characters, or more than two unicode characters, then there are more possible string values than possible integer values. You can't have a unique integer value for every possible 5 character string. Long values have more possible values, but they would only provide a unique value for every possible string of 8 ASCII characters.</p>\n\n<p>Hash codes are useful as a two step process: first see if the hash code matches, then check the whole string. For most strings that don't match, you only need to do the first step, and it's really fast.</p>\n"}, {'answer_id': 46224, 'author': 'Chris Upchurch', 'author_id': 2600, 'author_profile': 'https://Stackoverflow.com/users/2600', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>String length may vary, but let's say 10 characters for now.</p>\n</blockquote>\n\n<p>In that case, in order to guarantee uniqueness you'd have to use some sort of big integer representation.  I doubt that doing comparisons on big integers would be substantially faster than doing string comparisons in the first place.  I'll second what other's have said here, use some sort of hash, then in the event of a hash match check the original strings to weed out any collisions.</p>\n\n<p>In any case, If your strings are around 10 characters, I doubt that comparing, say, a bunch of 32 bit hashes will be all that much faster than direct string comparisons.  I think you have to ask yourself if it's it really worth the additional complexity.</p>\n"}, {'answer_id': 46247, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 2, 'selected': False, 'text': "<p>At the end of the day, a single alphanumeric character has at least 36 possible values.  If you include punctuation, lower case, etc then you can easily pass 72 possible values.</p>\n\n<p>A non-colliding number that allows you to quickly compare strings would necessarily grow exponentially with the length of the string.</p>\n\n<p>So you <em>first</em> must decide on the longest string you are expecting to compare.  Assuming it's N characters in length, and assuming you ONLY need uppercase letters and the numerals 0-9 then you need to have an integer representation that can be as high as\n36^N</p>\n\n<p>For a string of length 25 (common name field) then you end up needing a binary number with  130 bits.</p>\n\n<p>If you compose that into 32 bit numbers, you'll need 4.  Then you can compare each number (four integer compares should take no time, compared to walking the string).  I would recommend a big number library, but for this specialized case I'm pretty sure you can write your own and get better performance.</p>\n\n<p>If you want to handle 72 possible values per character (uppercase, lowercase, numerals, punctuation...) and you need 10 characters, then you'll need 62 bits - two 32 bit integers (or one 64 bit if you're on a system that supports 64 bit computing)</p>\n\n<p>If, however, you are not able to restrict the numbers in the string (ie, could be any of the 256 letters/numbers/characters/etc) and you can't define the size of the string, then comparing the strings directly is the only way to go, but there's a shortcut.</p>\n\n<p>Cast the pointer of the string to a 32 bit unsigned integer array, and compare the string 4 bytes at a time (or 64 bits/8bytes at a time on a 64 bit processor).  This means that a 100 character string only requires 25 compares maximum to find which is greater.</p>\n\n<p>You may need to re-define the character set (and convert the strings) so that the characters with higher precedence are assigned values closer to 0, and lower precedence values closer to 255 (or vice versa, depending on how you are comparing them).</p>\n\n<p>Good luck!</p>\n\n<p>-Adam</p>\n"}, {'answer_id': 47000, 'author': 'ckpwong', 'author_id': 2551, 'author_profile': 'https://Stackoverflow.com/users/2551', 'pm_score': 1, 'selected': False, 'text': "<p>As long as it's a hash function, be it String.hashCode(), MD5 or SHA1, collision is unavoidable unless you have a fixed limit on the string's length.  It is mathematically impossible to have one-to-one mapping from an infinite group to a finite group.</p>\n\n<p>Stepping back, is collision avoidance <em>absolutely</em> necessary?</p>\n"}]
['https://Stackoverflow.com/questions/46160', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4216/']