Java 使用两个字符串生成唯一标识符
我想了解如何使用两个字符串生成唯一标识符。这里我的要求是为特定文档生成唯一标识符。对于id生成,必须使用文档“名称”和“版本”。应该有一种方法可以从特定文档的唯一标识符中同时获取“名称”和“版本”。有没有在java中使用UUID来实现这一点的方法?或者最好的方法是什么。我们可以为此使用哈希或编码吗?如果可以,如何使用?我不知道为什么要使用两个字符串来生成唯一的ID,并且在某些情况下可能无法保持唯一性Java 使用两个字符串生成唯一标识符,java,uuid,Java,Uuid,我想了解如何使用两个字符串生成唯一标识符。这里我的要求是为特定文档生成唯一标识符。对于id生成,必须使用文档“名称”和“版本”。应该有一种方法可以从特定文档的唯一标识符中同时获取“名称”和“版本”。有没有在java中使用UUID来实现这一点的方法?或者最好的方法是什么。我们可以为此使用哈希或编码吗?如果可以,如何使用?我不知道为什么要使用两个字符串来生成唯一的ID,并且在某些情况下可能无法保持唯一性java.util.UUID为您的案例提供了有用的方法。看看这个用法: import java.u
java.util.UUID
为您的案例提供了有用的方法。看看这个用法:
import java.util.UUID;
...
UUID idOne = UUID.randomUUID();
UUID idTwo = UUID.randomUUID();
如果您对用这种方法生成的ID不满意,您可以在生成的ID中附加/前置附加您的附加参数,如名称和版本。最好的方法是使用通常不会出现在这些字符串中的分隔符连接字符串 e、 g
您可以使用
split(“/”)获取原始名称和版本您可以使用静态工厂方法randomUUID()
获取UUID
对象:
UUID id = UUID.randomUUID();
要将id与版本相结合,请使用一个分隔符,您知道它不会出现在任何一个字符串中,如/
或\uu
。然后,您可以在该分隔符上拆分
,或使用正则表达式提取所需内容:
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
}
如果不想创建UUID,只需将其保留为纯字符串即可
String.format("%s_%s_%s_%s", str1.length(), str2.length(), str1, str2);
用以下方法进行测试
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__'
这是因为开头创建了一种使用不能与数字一起存在的分隔符来解析以下内容的方法,如果使用类似“0”的分隔符,则解析将失败
其他示例依赖于两个字符串中都不存在的分隔符
str1+"."+str2
// both "..","." and ".",".." result in "...."
编辑
支持组合“n”字符串
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();
}
ID的要求是什么:(1)最大长度,(2)最小长度,(3)仅数字?根据您目前给出的要求,我的答案是:String id=name+“|”+version。欢迎使用so!你应该坐飞机。希望我们的答案是有帮助的。
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();
}