java 如何在Java中生成固定长度的唯一标识符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6584484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate a unique identifier of a fixed length in Java?
提问by Alfredo Osorio
I am trying to generate a unique identifier of a fixed length such as the IDs that are generated by Megaupload for the uploaded files.
我正在尝试生成固定长度的唯一标识符,例如 Megaupload 为上传的文件生成的 ID。
For example:
例如:
- ALGYTAB5
- BCLD23A6
- ALGYTAB5
- BCLD23A6
In this example using from A-Z and 0-9 and with a fixed length of 8 the total different combinations are 2,821,109,907,456.
在此示例中,使用来自 AZ 和 0-9 且固定长度为 8 的不同组合总数为 2,821,109,907,456。
What if one of the generated id is already taken. Those ids are going to be stored in a database and it shouldn't be used more than once.
如果生成的 id 之一已经被占用怎么办。这些 id 将存储在数据库中,不应多次使用。
How can I achieve that in Java?
我如何在 Java 中实现这一点?
Thank you.
谢谢你。
采纳答案by Armen Tsirunyan
Hmm... You could imitate a smaller GUID
the following way. Let first 4 bytes of your string be the encoded current time - seconds passed after Unix. And the last 4 just a random combination. In this case the only way two ID's would coincide is that they were built at the same second. And the chances of that would be very veeery low because of the other 4 random characters.
嗯...您可以GUID
通过以下方式模仿较小的。让字符串的前 4 个字节是编码的当前时间 - Unix 之后经过的秒数。而最后4个只是随机组合。在这种情况下,两个 ID 重合的唯一方法是它们是在同一秒内构建的。由于其他 4 个随机字符,这种可能性非常低。
Pseudocode:
伪代码:
get current time (4 byte integer
id[0] = 1st byte of current time (encoded to be a digit or a letter)
id[1] = 2nd
id[2] = 3rd
id[3] = 4th
id[4] = random character
id[5] = random character
id[6] = random character
id[7] = random character
回答by Lunf
I have tried @Armen's solution however I would like to give another solution
我已经尝试过@Armen 的解决方案,但是我想提供另一个解决方案
UUID idOne = UUID.randomUUID();
UUID idTwo = UUID.randomUUID();
UUID idThree = UUID.randomUUID();
UUID idFour = UUID.randomUUID();
String time = idOne.toString().replace("-", "");
String time2 = idTwo.toString().replace("-", "");
String time3 = idThree.toString().replace("-", "");
String time4 = idFour.toString().replace("-", "");
StringBuffer data = new StringBuffer();
data.append(time);
data.append(time2);
data.append(time3);
data.append(time4);
SecureRandom random = new SecureRandom();
int beginIndex = random.nextInt(100); //Begin index + length of your string < data length
int endIndex = beginIndex + 10; //Length of string which you want
String yourID = data.substring(beginIndex, endIndex);
Hope this help!
希望这有帮助!
回答by Sucheth Shivakumar
Follow these below steps:
请按照以下步骤操作:
- generateId()
- Check if it is already issued by querying database.
- If yes, call generateId() again until you do not find it in db
- 生成 ID()
- 通过查询数据库检查它是否已经发出。
- 如果是,再次调用 generateId() 直到在 db 中找不到它
回答by Thomas
We're using the database to check whether they already exist. If the number of IDs is low compared to the possible number you should be relatively safe.
我们正在使用数据库来检查它们是否已经存在。如果 ID 的数量与可能的数量相比较少,则您应该相对安全。
You might also have a look at the UUID
class (although it's 16-byte UUIDs).
您可能还会查看UUID
该类(尽管它是 16 字节的 UUID)。
回答by tskuzzy
Sounds like a job for a hash function. You're not 100% guaranteed that a hash function will return a unique identifier, but it works most of the time. Hash collisions must be dealt with separately, but there are many standard techniques for you to look into.
听起来像是哈希函数的工作。您不能 100% 保证哈希函数将返回唯一标识符,但它在大多数情况下都有效。散列冲突必须单独处理,但有许多标准技术可供您研究。
Specifically how you deal with collisions depends on what you're using this unique identifier for. If it's a simple one-way identifier where you give your program the ID and it returns the data, then you can simply use the next available ID in the case of a collision.
具体而言,您如何处理冲突取决于您使用此唯一标识符的目的。如果它是一个简单的单向标识符,您可以在其中为程序提供 ID 并返回数据,那么您可以在发生冲突时使用下一个可用的 ID。