本文翻译自:Create a GUID in Java
在Java中创建GUID的最佳方法有哪些?
#1楼
参考:https://stackoom.com/question/CVwq/在Java中创建GUID
#2楼
Have a look at the UUID class bundled with Java 5 and later. 看看与Java 5及更高版本捆绑在一起的UUID类 。
For example: 例如:
- If you want a random UUID you can use the randomUUID method. 如果需要随机UUID,可以使用randomUUID方法。
- If you want a UUID initialized to a specific value you can use the UUID constructor or the fromString method. 如果要将UUID初始化为特定值,可以使用UUID构造函数或fromString方法。
#3楼
java.util.UUID.randomUUID();
#4楼
It depends what kind of UUID you want. 这取决于你想要什么样的UUID 。
The standard Java
UUID
class generates Version 4 (random) UUIDs. 标准JavaUUID
类生成版本4 (随机)UUID。 ( UPDATE - Version 3 (name) UUIDs can also be generated.) It can also handle other variants, though it cannot generate them. ( 更新 - 版本3 (名称)UUID也可以生成。)它也可以处理其他变体,但它不能生成它们。 (In this case, "handle" means constructUUID
instances fromlong
,byte[]
orString
representations, and provide some appropriate accessors.) (在这种情况下,“handle”表示从long
,byte[]
或String
表示构造UUID
实例,并提供一些适当的访问器。)The Java UUID Generator (JUG) implementation purports to support "all 3 'official' types of UUID as defined by RFC-4122 " ... though the RFC actually defines 4 types and mentions a 5th type. Java UUID生成器(JUG)实现旨在支持“ RFC-4122定义的所有3种'官方'类型的UUID”......尽管RFC实际上定义了4种类型,但提到了第5种类型。
For more information on UUID types and variants, there is a good summary in Wikipedia , and the gory details are in RFC 4122 and the other specifications. 有关UUID类型和变体的更多信息, 维基百科中有一个很好的摘要,血清细节在RFC 4122和其他规范中。
#5楼
The other Answers are correct, especially this one by Stephen C . 其他的答案是正确的,尤其是这一个由斯蒂芬·Ç 。
Reaching Outside Java 到达Java之外
Generating a UUID value within Java is limited to Version 4 (random) because of security concerns. 出于安全考虑,在Java中生成UUID值仅限于版本4(随机) 。
If you want other versions of UUIDs, one avenue is to have your Java app reach outside the JVM to generate UUIDs by calling on: 如果您需要其他版本的UUID,一个途径是让您的Java应用程序到达JVM外部以通过调用来生成UUID:
- Command-line utility 命令行实用程序
Bundled with nearly every operating system. 几乎与每个操作系统捆绑在一起。
For example,uuidgen
found in Mac OS X, BSD, and Linux. 例如,uuidgen
在Mac OS X,BSD和Linux中找到。 - Database server 数据库服务器
Use JDBC to retrieve a UUID generated on the database server. 使用JDBC检索在数据库服务器上生成的UUID。
For example, theuuid-ossp
extension often bundled with Postgres . 例如,uuid-ossp
扩展通常与Postgres捆绑在一起。 That extension can generates Versions 1, 3, and 4 values and additionally a couple variations: 该扩展可以生成版本1,3和4值,还有几个变体:-
uuid_generate_v1mc()
– generates a version 1 UUID but uses a random multicast MAC address instead of the real MAC address of the computer.uuid_generate_v1mc()
- 生成版本1 UUID但使用随机多播MAC地址而不是计算机的实际MAC地址。 -
uuid_generate_v5(namespace uuid, name text)
– generates a version 5 UUID, which works like a version 3 UUID except that SHA-1 is used as a hashing method.uuid_generate_v5(namespace uuid, name text)
- 生成版本5 UUID,其工作方式类似于版本3 UUID,但SHA-1用作散列方法。
-
- Web Service 网络服务
For example, UUID Generator creates Versions 1 & 3 as well as nil values and GUID . 例如, UUID Generator创建版本1和3以及nil值和GUID 。
#6楼
Just to extend Mark Byers's answer with an example: 只是用一个例子来扩展Mark Byers的答案:
import java.util.UUID;
public class RandomStringUUID {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
System.out.println("UUID=" + uuid.toString() );
}
}