问题
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?
回答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.
回答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("/")
回答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
}
回答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();
}
来源:https://stackoverflow.com/questions/17030498/generating-unique-identifier-using-two-strings