在Java中生成UUID字符串的有效方法(UUID.randomUUID().toString(),不带破折号)
我想要一个有效的实用程序来生成唯一的字节序列。 UUID 是一个很好的候选者,但是 UUID.randomUUID().toString()
生成类似 44e128a5-ac7a-4c9a-be4c-224b6bf81b20
的东西,这很好,但我更喜欢破折号- 少字符串。
我正在寻找一种仅从字母数字字符(没有破折号或任何其他特殊符号)生成随机字符串的有效方法。
I would like an efficient utility to generate unique sequences of bytes. UUID is a good candidate but UUID.randomUUID().toString()
generates stuff like 44e128a5-ac7a-4c9a-be4c-224b6bf81b20
which is good, but I would prefer dash-less string.
I'm looking for an efficient way to generate a random strings, only from alphanumeric characters (no dashes or any other special symbols).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这是这样做的:
This does it:
正如您在此线程的 URL 中看到的那样,不需要从 HTTP 请求中删除破折号。
但是,如果您想准备格式良好的 URL 而不依赖于数据,则应该使用 URLEncoder.encode( String data, String encoding ) 而不是更改数据的标准形式。
对于 UUID 字符串表示,破折号是正常的。
Dashes don't need to be removed from HTTP request as you can see in URL of this thread.
But if you want to prepare well-formed URL without dependency on data you should use URLEncoder.encode( String data, String encoding ) instead of changing standard form of you data.
For UUID string representation dashes is normal.
我使用JUG(Java UUID Generator)来生成唯一ID。
它在 JVM 中是独一无二的。用起来相当不错。以下是供您参考的代码:
您可以从以下位置下载该库: https://github.com/ cowtowncoder/java-uuid-generator
I used JUG (Java UUID Generator) to generate unique ID.
It is unique across JVMs. Pretty good to use. Here is the code for your reference:
You could download the library from: https://github.com/cowtowncoder/java-uuid-generator
一个简单的解决方案是
(与现有解决方案一样,只是它避免了 String#replaceAll 调用。这里不需要正则表达式替换,所以 String#replace 感觉更自然,尽管从技术上讲它仍然是使用正则表达式实现的。鉴于 UUID 的生成比替换的成本更高,因此运行时不应有显着差异。)
对于大多数情况,使用 UUID 类可能足够快,但我希望如此一些专门的手写变体,不需要后处理,速度更快。无论如何,整个计算的瓶颈通常是随机数生成器。对于 UUID 类,它使用 SecureRandom。
使用哪个随机数生成器也是一个取决于应用程序的权衡。如果对安全性敏感,通常建议使用 SecureRandom。否则, ThreadLocalRandom是一种替代方案(比 SecureRandom 或旧的 Random,但不是加密安全的)。
A simple solution is
(Like the existing solutions, only that it avoids the String#replaceAll call. Regular expression replacement is not required here, so String#replace feels more natural, though technically it still is implemented with regular expressions. Given that the generation of the UUID is more costly than the replacement, there should not be a significant difference in runtime.)
Using the UUID class is probably fast enough for most scenarios, though I would expect that some specialized hand-written variant, which does not need the postprocessing, to be faster. Anyway, the bottleneck of the overall computation will normally be the random number generator. In case of the UUID class, it uses SecureRandom.
Which random number generator to use is also a trade-off that depends on the application. If it is security-sensitive, SecureRandom is, in general, the recommendation. Otherwise, ThreadLocalRandom is an alternative (faster than SecureRandom or the old Random, but not cryptographically secure).
最终根据 UUID.java 实现编写了我自己的一些东西。请注意,我没有生成 UUID,而是以我能想到的最有效的方式生成随机的 32 字节十六进制字符串。
实施
使用
测试
我测试了一些输入以确保其正常工作:
Ended up writing something of my own based on UUID.java implementation. Note that I'm not generating a UUID, instead just a random 32 bytes hex string in the most efficient way I could think of.
Implementation
Usage
Tests
Some of the inputs I've tested to make sure it's working:
我很惊讶看到这么多字符串替换 UUID 的想法。怎么样:
这是执行此操作的快速方法,因为 UUID 的整个 toString() 已经更加昂贵,更不用说必须解析和执行的正则表达式或用空字符串替换。
I am amazed to see so many string replace ideas of UUID. How about this:
This is the fasted way of doing it since the whole toString() of UUID is already more expensive not to mention the regular expression which has to be parsed and executed or the replacing with empty string.
我刚刚复制了 UUID toString() 方法并更新了它以从中删除“-”。它将比任何其他解决方案更快、更直接
用法:
generateUUIDString(UUID.randomUUID())
使用反射的另一种实现
I have just copied UUID toString() method and just updated it to remove "-" from it. It will be much more faster and straight forward than any other solution
Usage:
generateUUIDString(UUID.randomUUID())
Another implementation using reflection
我使用 org.apache.commons.codec.binary.Base64 将 UUID 转换为 url 安全的唯一字符串,该字符串长度为 22 个字符,并且与 UUID 具有相同的唯一性。
我在将UUID存储为base64字符串上发布了我的代码
I use org.apache.commons.codec.binary.Base64 to convert a UUID into a url-safe unique string that is 22 characters in length and has the same uniqueness as UUID.
I posted my code on Storing UUID as base64 String
好吧,由于 UUID 在 toString() 上添加连字符(破折号),我们可以从 Java 自己的实现中窃取实现,将字节数组缩短为 32 并调整偏移量。
运行它:
生成:
实际上应该与 Java 一样高效(更高效)。
Well, since an UUID gets added the hyphens (dashes) on toString() we can steal the implementation from Java's own implementation, shorting the byte array to 32 and adjusting the offset.
Running it:
Generates:
Should be just as efficient (more effecient) than Javas actually.
该实用程序类将 UUID 生成为带或不带破折号的字符串。
这是输出:
uuid-creator 中有一个编解码器可以更有效地完成此操作:< a href="https://github.com/f4b6a3/uuid-creator/blob/master/src/main/java/com/github/f4b6a3/uuid/codec/base/Base16Codec.java" rel="nofollow noreferrer" >
Base16Codec
。例子:This utility class generates UUIDs as String with or without dashes.
This is the output:
There's a codec in uuid-creator that can do it more efficiently:
Base16Codec
. Example: