Create a unique identifier using two lines

I would like to get an idea of ​​how we can generate a unique identifier using two lines. My requirement here is to create a unique identifier for a specific document. To generate id, you must use the document name and "version". And there must be a way to return the "name" and "version" from the unique identifier of a particular document. Is there a way to do this using the UUID in java? or what is the best way to do this. Can we use hashing or coding for this purpose, and if so, how?

+4
source share
5 answers

I do not know why you want to use 2 lines to create a unique identifier, and in some cases it may not be possible to preserve uniqueness. 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 identifiers generated in this way, you can add / add your additional parameters, such as name and version, to the generated identifiers.

+2
source

The best way to do this is to concatenate the lines with a separator, which usually does not appear on these lines

eg.

 String name = .... String version = ..... String key = name + "/" + version; 

You can get the original name and version with split("/")

0
source

You can use the static randomUUID() static method to get the UUID object:

 UUID id = UUID.randomUUID(); 

To combine the identifier with the version, come up with a separator, which, as you know, will not be on any line, like / or _ . You can then split on this separator or use regular expressions to extract what you want:

 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 } 
0
source

If you do not want to create UUIDs, just keep it clean

 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 for parsing the next use using a separator that cannot exist with numbers; it won’t work if you use a separator like β€œ0”

Other examples are based on a delimiter that does not exist on any line.

 str1+"."+str2 // both "..","." and ".",".." result in "...." 

EDIT

Support for joining strings '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(); } 
0
source
 String one = "One"; String two = "Two"; java.util.UUID val = UUID.fromString(one.concat(two)); 
-1
source

Source: https://habr.com/ru/post/1485494/


All Articles