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
To satisfy the requirement
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
Strings, which is something like $255^30$ in your case (or something like $65536^30$ if you allow arbitrary Unicode characters). So you’ll needBigIntegers for that and certainly you useint(simply there are moreStrings of length up to 30 than numbers inint). For example,new BigInteger(theString.getBytes(""))satisfies your requirement.If you use
hashCode, you’ll lose injectivity, but the chance that twoStrings will have the samehashCodeis 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.