'Generating Unique identifier using two strings
I would like to get an idea on how can we generate an unique identifier using two Strings. My requirement here is to generate an unique identifier for a particular document. For id generation, document 'name' and 'version' has to be used. And there should be a way to get back both 'name' and 'version' from the unique identifier of a particular document. Is there a way to do this using UUID in java? or what is the best way of doing this. Can we use hashing or encoding for this purpose and if so how?
Solution 1:[1]
I don't know why you want to use 2 strings to generate a unique ID and it might not be possible to preserve uniqueness in some cases. java.util.UUID
presents useful methods for your case. Take a look at this usage:
import java.util.UUID;
...
UUID idOne = UUID.randomUUID();
UUID idTwo = UUID.randomUUID();
If you are not satisfied with the IDs generated with this way, you can append / prepend your additional parameters like name and version to the generated IDs.
Solution 2:[2]
The best way to do this is to concatenate the strings with a separator which would not normally appear in those Strings
e.g.
String name = ....
String version = .....
String key = name + "/" + version;
You can obtain the original name and version with split("/")
Solution 3:[3]
You can use the static factory method randomUUID()
to get a UUID
object:
UUID id = UUID.randomUUID();
To combine the id with the version, come up with a delimiter that you know won't be in either string, like /
or _
. Then you can split
on that delimiter or use regular expressions to extract what you desire:
String entry = id + "_" + version;
String[] divided = entry.split(delimiter);
//or using regex
String entry= "idName_version"; //delimiter is "_"
String pattern = "(.*)_(.*)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(example);
if (m.find())
{
System.out.println(m.group(1)); //prints idName
System.out.println(m.group(2)); //prints version
}
Solution 4:[4]
If you don't want to create a UUID just keep it pure strings
String.format("%s_%s_%s_%s", str1.length(), str2.length(), str1, str2);
tested with the following
Pair.of("a", ""), // 'a' and '' into '1_0_a_'
Pair.of("", "a"), // '' and 'a' into '0_1__a'
Pair.of("a", "a"), // 'a' and 'a' into '1_1_a_a'
Pair.of("aa", "a"), // 'aa' and 'a' into '2_1_aa_a'
Pair.of("a", "aa"), // 'a' and 'aa' into '1_2_a_aa'
Pair.of("_", ""), // '_' and '' into '1_0___'
Pair.of("", "_"), // '' and '_' into '0_1___'
Pair.of("__", "_"), // '__' and '_' into '2_1_____'
Pair.of("_", "__"), // '_' and '__' into '1_2_____'
Pair.of("__", "__"), // '__' and '__' into '2_2______'
Pair.of("/t/", "/t/"), // '/t/' and '/t/' into '3_3_/t/_/t/'
Pair.of("", "") // '' and '' into '0_0__'
This works because the beginning creates a way to parse the following using a delimiter that cannot exist with numbers, it would fail if one used a delimiter like '0'
The other examples rely on the delimiter not existing in either string
str1+"."+str2
// both "..","." and ".",".." result in "...."
EDIT
Support for combining 'n' strings
private static String getUniqueString(String...srcs) {
StringBuilder uniqueCombo = new StringBuilder();
for (String src : srcs) {
uniqueCombo.append(src.length());
uniqueCombo.append('_');
}
// adding this second _ between numbers and strings allows unique strings across sizes
uniqueCombo.append('_');
for (String src : srcs) {
uniqueCombo.append(src);
uniqueCombo.append('_');
}
return uniqueCombo.toString();
}
Solution 5:[5]
The best way of preserving the uniqueness of identifiers using two strings is to first check their hashcode and then join them based upon any condition you want either lower or greater. in the end, you will always get the same unique Key each time.
void main() {
print(join('String1', 'String2'));
print(join('String2', 'String1'));
print(join('Hello', 'World'));
print(join('World', 'Hello'));
}
String join (String str1, String str2) {
String result = '';
if (str1.hashCode <= str2.hashCode) {
result = '$str1-$str2';
} else {
result = '$str2-$str1';
}
return result;
}
Same as the above code you can order the strings and then do perform any operations to get a unique key.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Juvanis |
Solution 2 | Peter Lawrey |
Solution 3 | |
Solution 4 | |
Solution 5 | C.M Talha |