如何在Java中创建用户友好的唯一ID,UUID或其他唯一标识符 [英] How to create user friendly unique IDs, UUIDs or other unique identifiers in Java

查看:826
本文介绍了如何在Java中创建用户友好的唯一ID,UUID或其他唯一标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用UUID类生成唯一的ID.如果这些ID仅由技术系统使用,则无需担心它们的使用时间有多长:

I usually use the UUID class to generate unique IDs. This works fine if these IDs are used by technical systems only, they don't care how long they are:

System.out.println(UUID.randomUUID().toString());

> 67849f28-c0af-46c7-8421-94f0642e5d4d

是否存在一种创建比UUID短一点的用户友好的唯一ID(例如来自tinyurl的ID)的好方法?用例:您希望通过Mail将ID发送给客户,然后客户又访问您的网站,并将其输入到表单中,例如凭证ID.

Is there a nice way to create user friendly unique IDs (like those from tinyurl) which are a bit shorter than the UUIDs? Usecase: you want to send out IDs via Mail to your customers which in turn visit your site and enter that number into a form, like a voucher ID.

我假设在UUID的128位范围的整个范围内均等地生成了UUID.那么仅使用较低的64位会很明智吗?

I assume that UUIDs get generated equally through the whole range of the 128 Bit range of the UUID. So would it be sage to use just the lower 64 Bits for instance?

System.out.println(UUID.randomUUID().getLeastSignificantBits());

欢迎任何反馈.

推荐答案

我假设生成了UUID 在整个范围内 UUID的128位范围.

I assume that UUIDs get generated equally through the whole range of the 128 Bit range of the UUID.

首先,您的假设可能不正确,具体取决于UUID类型(1、2、3或4).来自 Java UUID文档:

First off, your assumption may be incorrect, depending on the UUID type (1, 2, 3, or 4). From the Java UUID docs:

存在以下不同的变体 这些全局标识符.方法 这堂课是为了操纵 Leach-Salz变体,尽管 构造函数允许创建任何 UUID的变体(如下所述).

There exist different variants of these global identifiers. The methods of this class are for manipulating the Leach-Salz variant, although the constructors allow the creation of any variant of UUID (described below).

变体2(Leach-Salz)的布局 UUID如下: 大量由 以下未签名的字段:

The layout of a variant 2 (Leach-Salz) UUID is as follows: The most significant long consists of the following unsigned fields:

0xFFFFFFFF00000000 time_low 
0x00000000FFFF0000 time_mid 
0x000000000000F000 version 
0x0000000000000FFF time_hi  

最不重要的long包括 以下未签名的字段:

The least significant long consists of the following unsigned fields:

0xC000000000000000 variant 
0x3FFF000000000000 clock_seq 
0x0000FFFFFFFFFFFF node  

变体字段包含一个值 它标识了 UUID.上面描述的位布局 仅对带有的UUID有效 变量值2,表示 Leach-Salz变种.

The variant field contains a value which identifies the layout of the UUID. The bit layout described above is valid only for a UUID with a variant value of 2, which indicates the Leach-Salz variant.

版本字段包含一个值,该值 描述此UUID的类型.那里 是四种不同的基本类型 UUID:基于时间的DCE安全性 基于名称,并随机生成 UUID.这些类型都有版本 值分别为1、2、3和4.

The version field holds a value that describes the type of this UUID. There are four different basic types of UUIDs: time-based, DCE security, name-based, and randomly generated UUIDs. These types have a version value of 1, 2, 3 and 4, respectively.

执行操作的最佳方法是使用如下所示的代码生成随机字符串(

The best way to do what you're doing is to generate a random string with code that looks something like this (source):

public class RandomString {

          public static String randomstring(int lo, int hi){
                  int n = rand(lo, hi);
                  byte b[] = new byte[n];
                  for (int i = 0; i < n; i++)
                          b[i] = (byte)rand('a', 'z');
                  return new String(b, 0);
          }

          private static int rand(int lo, int hi){
                      java.util.Random rn = new java.util.Random();
                  int n = hi - lo + 1;
                  int i = rn.nextInt(n);
                  if (i < 0)
                          i = -i;
                  return lo + i;
          }

          public static String randomstring(){
                  return randomstring(5, 25);
          }

        /**
         * @param args
         */
        public static void main(String[] args) {
                System.out.println(randomstring());

        }

}

如果您非常担心碰撞或其他问题,建议您base64对UUID进行编码,以减小其大小.

If you're incredibly worried about collisions or something, I suggest you base64 encode your UUID which should cut down on its size.

故事的寓意:因为UUID是经过整体设计的,所以不要依赖UUID的各个部分.如果确实需要依赖UUID的各个部分,请确保熟悉特定的UUID类型和实现.

Moral of the story: don't rely on individual parts of UUIDs as they are holistically designed. If you do need to rely on individual parts of a UUID, make sure you familiarize yourself with the particular UUID type and implementation.

这篇关于如何在Java中创建用户友好的唯一ID,UUID或其他唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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