在Java中创建GUID

本文翻译自: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: 例如:


#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. 标准Java UUID类生成版本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 construct UUID instances from long , byte[] or String representations, and provide some appropriate accessors.) (在这种情况下,“handle”表示从longbyte[]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, the uuid-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() );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值