Jul 12, 2008 And you might want to store it as a string in your database (for whatever reason). In C#, you can do this by using the GUID hash code. Here is the function that returns a 'unique enough' string with a desired length: /// /// Gets a unqiue key based on GUID string hash code. Jul 12, 2008 C#: Get A Unique Random String Using GUID Hash Code. GUID, also known as Globally Unique Identifier, is an 128-bit integer (16 bytes) that can be used to uniquely identify a record in the database (other than the auto-increase number). It is best used as a primary key as well.
Ranch Hand
posted 10 years ago
In Java, how can a create a Unique ID from a particular string?
I have gone through the UUID class but could not find anything, also googled this but to find nothing. Can any one please Help me on this?
Ranch Hand
posted 10 years ago
Try permutation and combination of a string element appended with some Randomized generated string/number.
[LEARNING bLOG] | [Freelance Web Designer] | [and 'Rohan' is part of my surname]
Ranch Hand
posted 10 years ago
Try permutation and combination of a string element appended with some Randomized generated string/number.
I think this doesn't work, because, the next time when i want to generate the Unique ID for the same string then this randomized generation doesn't work as the same Unique id doesn't get generated.
Author
posted 10 years ago
Sounds like you want a guaranteed hash code for a string, that's guaranteed to be 'shorter' than the string. Not sure how possible that will be--what is the reason for this requirement?
Marshal
posted 10 years ago
Too difficult a question for us beginners. Moving.
Ranch Hand
posted 10 years ago
In Java 1.5, static UUID UUID.fromString(String name);
As mentioned previously, what about the hashcode converted into a String? Integer.toString( myString.hashcode() );
Ranch Hand
posted 10 years ago
In Java 1.5, static UUID UUID.fromString(String name);
Here fromString creates a UUID for a valid UUID string and not any string.
Ranch Hand
posted 10 years ago
The String class may not have a hashCode() method, but Object does, and String is a subclass of Object. The same goes for Integer class and toString().
D'oh, previous post was edited. I'm still leaving my post up though.
SCJA
When I die, I want people to look at me and say 'Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was.'
Marshal
posted 10 years ago
Bharadwaj Adepu wrote:Here one more thing is the length of the generated Unique ID must be less than the original String.
So how should this work for the empty string? (There are no strings with length less than zero.) And how should it work for strings of one character? (All strings of length zero are the same, so you can't have a unique representation.)
Ranch Hand
posted 10 years ago
Integer class doesn't have toString(java.lang.String) Method and String class doesn't have any hashcode() method. Golang generate rsa key pem public. Warning rsa rsa_generate_key is depreciated.
From java.lang.Integer: public static String toString(int i) A simple method name typo. java.lang.String: public int hashCode() But this doesn't meet your later criteria of unique ID length less then String length. It would seem that you need to write your own mapping function.
Ranch Hand
posted 10 years ago
So how should this work for the empty string? (There are no strings with length less than zero.)
Here in my usage/application, i'll not create the Unique id for empty strings. Generate String From RegexHere actually what i want to do is, insert the generated unique id in the database, so if length of my string is more than 4000 and if generated unique string will have the length same as the original string(larger than 4000) then i cant insert that.
Ranch Hand
posted 10 years ago @Fred
Integer.toString( myString.hashCode() );
I think this serves my purpose, i have tested this and the hashCode generated is an number of around 10 to 12 digits, some times negative. Thanks a LOT But am not sure if this method creates a same hashCode for same string, i have tested this but not sure..
Author
posted 10 years ago
Bharadwaj Adepu wrote:But am not sure if this method creates a same hashCode for same string, i have tested this but not sure..
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#hashCode() AFAIK it's not guaranteed to be unique.
Ranch Hand
posted 10 years ago
Bharadwaj Adepu wrote: But am not sure if this method creates a same hashCode for same string, i have tested this but not sure.. If you read the contract of the hashCode method, it states:
http://java.sun.com/javase/6/docs/api/java/lang/Object.html#hashCode() wrote: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
This means that it will not necessarily produce the same hash code for the same object between runs of your JVM. Also, you stated you need a Unique ID for each String. Again, reading the hashCode contract, we learn:
http://java.sun.com/javase/6/docs/api/java/lang/Object.html#hashCode() wrote: It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
So there is no guarantee that you will get a unique ID for two different Strings. Although the odds are low, there is a possibility you might get the same ID for two different Strings. The documentation for the String's overriding of the hashCode method does not give any indication that these warnings do not apply to its implementation. (I have not studied the String class's implementation to know either way.) Common sense would indicate that given that you are dealing with Strings of 4000 characters (8000 bytes) and bigger that you can not uniquely represent all possible permutations in only 4 bytes (the size of an int). It sounds to me what you want is a compression method/process and not necessarily a 'unique ID'. You want to be able to store Strings over 4000 characters in less space, and always want the same result for the same string. Do you need the ability to reverse for this 'ID' to the original String? Or just get the same ID so you can look up related data in the database? I would suggest you look into compression methods and APIs.
Ranch Hand
posted 10 years ago
p.s. Of course another option might be to change your database field's data type to a CLOB rather than a VARCHAR(4000). It would probably be much easier and more reliable in the long run.
best scout
posted 10 years ago
Hi Bharadwaj,
I think what you need is a hash function. In general any kind of hash is a surjective projection (hope this is the correct mathematical name for it in English) which in your case means you're trying to map a given string consisting of a (large) number of characters to some kind of value which consists of a much smaller number of characters or bytes. This simply can't be done without collisions! Maybe you can reduce the size of the original string to some degree without loss with a compression function. But I guess it won't be possible to compress 4000 characters to 4 bytes without some information loss :-) So obviously the only way to go is to use a hash function and even if it theoretically may sound like a problem to have collisions it's often not a problem in reality. In fact there may be collisions which will map different strings to the same hash code. The only question is how likely it is for a collision to happen! A good cryptographic hash function should really make it very unlikely to get collisions. Of course you have to decide what 'good enough' means regarding your requirements! Marco
Ranch Hand
posted 10 years ago
Bharadwaj Adepu wrote:
Try permutation and combination of a string element appended with some Randomized generated string/number.
Generate String Of LengthI think this doesn't work, because, the next time when i want to generate the Unique ID for the same string then this randomized generation doesn't work as the same Unique id doesn't get generated.of course, it will work if you append/prepend/insert the random generated integer. And for the same string, I don't think the random generated number is same. Generate Random Character String
Bharadwaj Adepu wrote: Here one more thing is the length of the generated Unique ID must be less than the original String. C# Keys To StringWith permutations you can restrict the length of resulted string.
[LEARNING bLOG] | [Freelance Web Designer] | [and 'Rohan' is part of my surname]
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |