在Java中创建GUID的一些最佳方法是什么?
当前回答
看看与Java 5及更高版本捆绑在一起的UUID类。
例如:
如果你想要一个随机的UUID,你可以使用randomUUID方法。 如果您希望将UUID初始化为特定的值,可以使用UUID构造函数或fromString方法。
其他回答
用一个例子来扩展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() );
}
}
这取决于你想要什么样的UUID。
The standard Java UUID class generates Version 4 (random) UUIDs. (UPDATE - Version 3 (name) UUIDs can also be generated.) It can also handle other variants, though it cannot generate them. (In this case, "handle" means construct UUID instances from long, byte[] or String representations, and provide some appropriate accessors.) 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.
关于UUID类型和变体的更多信息,维基百科中有一个很好的总结,RFC 4122和其他规范中有详细的信息。
java.util.UUID.randomUUID();
在许多情况下,我们需要对象的全局UUID,特别是在事件驱动架构或事件源中,我们必须根据日期对事件进行排序,但我们不需要关于时间戳的完整信息。
在那里,我们可以使用ULID的一个实现,它是按字典顺序排序的。
格式与标准UUID不同,但仍然很简单:
example value: 01AN4Z07BY79KA1307SR9X4MV3
01AN4Z07BY 79KA1307SR9X4MV3
|----------| |----------------|
Timestamp Randomness
48bits 80bits
在许多语言中都有实现。
例如,在Java中,有一个简单的库。
代码示例:
import de.huxhorn.sulky.ulid.ULID;
ULID ulid = new ULID();
// with current timestamp
String newId = ulid.nextULID();
// with selected timestamp
String newId2 = ulid.nextULID(Instant
.parse("2021-12-01T00:00:00.00Z")
.toEpochMilli()
);
使用Spring,您还可以为ULID生成器创建Bean。
@Configuration
public class UUIDGeneratorConfig {
@Bean
public ULID ulidGenerator() {
return new ULID();
}
}
@Component
public class ULIDGenerator {
private final ULID ulid;
public ULIDGenerator(ULID ulid) {
this.ulid = ulid;
}
public String generateUUID() {
return ulid.nextULID();
}
public String generateUUID(Instant timestamp) {
return ulid.nextULID(timestamp.toEpochMilli());
}
}
其他答案都是正确的,尤其是斯蒂芬·C的这个答案。
走出Java
出于安全考虑,在Java中生成UUID值仅限于版本4(随机)。
如果你想要其他版本的uuid,一种方法是让你的Java应用程序通过调用JVM之外的方法来生成uuid:
Command-line utilityBundled with nearly every operating system. For example, uuidgen found in Mac OS X, BSD, and Linux. Database serverUse JDBC to retrieve a UUID generated on the database server.For example, the uuid-ossp extension often bundled with Postgres. That extension can generates Versions 1, 3, and 4 values and additionally a couple variations: 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_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. Web ServiceFor example, UUID Generator creates Versions 1 & 3 as well as nil values and GUID.
推荐文章
- Java构造函数继承
- 如何在Java中将int[]转换为Integer[] ?
- 你能解释一下流的概念吗?
- 导致java.lang.VerifyError错误的原因
- 如何在Java中监控计算机的CPU、内存和磁盘使用情况?
- 如何设置超时在改造库?
- java lambda可以有一个以上的参数吗?
- HashMap -获取第一个键值
- 使用Jackson将JSON字符串转换为漂亮的打印JSON输出
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- Javadoc @see或{@link}?
- 在准备语句中使用“like”通配符
- Android Eclipse -无法找到*.apk
- javac和Eclipse编译器之间的区别是什么?
- 工厂模式和策略模式之间的区别是什么?