使用两个字符串生成唯一标识符 [英] Generating Unique identifier using two strings

查看:437
本文介绍了使用两个字符串生成唯一标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用两个字符串生成唯一标识符。我的要求是为特定文档生成唯一标识符。对于id生成,必须使用文档'name'和'version'。应该有办法从特定文档的唯一标识符中取回名称和版本。有没有办法在java中使用UUID?或者这样做的最佳方式是什么。我们可以为此目的使用散列或编码吗?如果是这样的话?

I would like to get an idea on how can we generate an unique identifier using two Strings. My requirement here is to generate an unique identifier for a particular document. For id generation, document 'name' and 'version' has to be used. And there should be a way to get back both 'name' and 'version' from the unique identifier of a particular document. Is there a way to do this using UUID in java? or what is the best way of doing this. Can we use hashing or encoding for this purpose and if so how?

推荐答案

我不知道你为什么要使用2用于生成唯一ID的字符串,在某些情况下可能无法保留唯一性。 java.util.UUID 为您的案例提供了有用的方法。看看这个用法:

I don't know why you want to use 2 strings to generate a unique ID and it might not be possible to preserve uniqueness in some cases. java.util.UUID presents useful methods for your case. Take a look at this usage:

import java.util.UUID;

...

UUID idOne = UUID.randomUUID();
UUID idTwo = UUID.randomUUID();

如果您对以这种方式生成的ID不满意,可以追加/前置您生成的ID等名称和版本等附加参数。

If you are not satisfied with the IDs generated with this way, you can append / prepend your additional parameters like name and version to the generated IDs.

这篇关于使用两个字符串生成唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆