Reputation: 1009
Giving a Input String, the string length will not more than 30, the output will be a unique id number. Is there a way in Java can do this?the same string will always generate the same id, different string can not generate the same id. the java HashCode() can do this ?
thanks
Upvotes: 2
Views: 1382
Reputation: 63409
To satisfy the requirement
the same string will always generate the same id, different string can not generate the same id
you'll get pretty huge numbers. You require that the function will be injective so you need as many numbers as the number of possible String
s, which is something like $255^30$ in your case (or something like $65536^30$ if you allow arbitrary Unicode characters). So you'll need BigInteger
s for that and certainly you use int
(simply there are more String
s of length up to 30 than numbers in int
). For example, new BigInteger(theString.getBytes(""))
satisfies your requirement.
If you use hashCode
, you'll lose injectivity, but the chance that two String
s will have the same hashCode
is very low in most cases (it's actually the purpose of hashing to make this chance low). If you want to be extra sure that the number of collisions uniformly small, you can use some cryptographic hash function, but still, the mapping won't be injective.
Perhaps explaining the reasons for your requirements would help finding the best solution.
Upvotes: 3
Reputation: 20264
If you really truly need to be sure there are no collisions then the numbers you get will be enormous, certainly bigger than any of the primitive numeric types. My advice would be to use a SHA-1 hash which is almost certainly good enough (the Git version control system relies on the uniqueness of these hash values, are your requirements really stricter than theirs?)
If you want to guarantee uniqueness then I suppose one way you could do it would be to take the ASCII (or Unicode, depending on your input) numeric value for each character, padding it with zeroes so that all values have the same length, and then concatenating them all together into one big number. As stated previously there is no way you will fit this into a long value, so you will need to use the BigInteger class. The padding will be necessary to avoid collisions like '12 + 34' and '123 + 4'.
Upvotes: 0
Reputation: 18236
Unless you can restrict the set of valid strings in a deterministic way your only option is to interpret the whole string as your id. As you're going to obtain very large id's you should use the BigInteger class to represent them.
I'd convert characters in reverse order, so as not to have to worry about strings of different length.
Upvotes: 0
Reputation: 152294
Maybe it is a bit silly, but how about converting every character to ASCII number ?
Upvotes: 0
Reputation: 5145
Java hashcode is not guaranteed to be unique. You need to look into UUID.
public static UUID nameUUIDFromBytes(byte[] name)
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/UUID.html
Upvotes: 1