Generating Unique identifier using two strings

ぃ、小莉子 提交于 2019-12-01 18:44:29

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.

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("/")

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
}

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();
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!