简体   繁体   English

如何减少使用 randomUUID( ) 生成的 UUID 的长度

[英]how to reduce length of UUID generated using randomUUID( )

I have a short utility in which I am generating a UUID using randomUUID().我有一个简短的实用程序,我在其中使用 randomUUID() 生成一个 UUID。

String uuid = UUID.randomUUID().toString();

However, the uuid generated is too long which is 36 in length.但是,生成的 uuid 太长,长度为 36。

Is there any way I can reduce the length of the UUID from 36 to near 16 or make the UUID length dynamic?有什么办法可以将 UUID 的长度从 36 减少到接近 16 或使 UUID 长度动态化?

If you don't need it to be unique, you can use any length you like.如果你不需要它是独一无二的,你可以使用任何你喜欢的长度。

For example, you can do this.例如,您可以这样做。

Random rand = new Random();
char[] chars = new char[16];
for(int i=0;i<chars.length;i++) {
    chars[i] = (char) rand.nextInt(65536);
    if (!Character.isValidCodePoint(chars[i]))
        i--;
}
String s = new String(chars);

This will give you almost the same degree of randomness but will use every possible character between \ and \�这将为您提供几乎相同程度的随机性,但将使用\\�之间的每个可能的字符

If you need printable ASCII characters you can make it as short as you like but the likelihood of uniqueness drops significantly.如果您需要可打印的 ASCII 字符,您可以让它尽可能短,但唯一性的可能性会显着下降。 What can do is use base 36 instead of base 16可以做的是使用 base 36 而不是 base 16

UUID uuid = UUID.randomUUID();
String s = Long.toString(uuid.getMostSignificantBits(), 36) + '-' + Long.toString(uuid.getLeastSignificantBits(), 36);

This will 26 characters on average, at most 27 character.这将平均 26 个字符,最多 27 个字符。

You can use base64 encoding and reduce it to 22 characters.您可以使用 base64 编码并将其减少到 22 个字符。

If you use base94 you can get it does to 20 characters.如果您使用 base94,您可以获得 20 个字符。

If you use the whole range of valid chars fro \ to \� you can reduce it to just 9 characters or 17 bytes.如果您使用从 \ 到 \� 的整个有效字符范围,您可以将其减少到仅 9 个字符或 17 个字节。

If you don't care about Strings you can use 16, 8-bit bytes.如果您不关心字符串,则可以使用 16 位 8 位字节。

You can use the substring method to decrease the string length while generating uuid.您可以使用 substring 方法在生成 uuid 时减少字符串长度。

UUID.randomUUID().toString().substring(0, 5)

String uuid = String.format("%040d", new BigInteger(UUID.randomUUID().toString().replace("-", ""), 16));

String uuid16digits = uuid.substring(uuid.length() - 16);

这将返回实际 uuid 的最后 16 位数字。

您可以使用${__time()}函数进行评估

Convert it from base 16(0-9,AF) to base 36(0-9,AZ).. You could go to base 62 (0-9, AZ, az) but if you need to read it over a phone or something then this can be error prone.将它从基数 16(0-9,AF) 转换为基数 36(0-9,AZ)。您可以转到基数 62 (0-9, AZ, az) 但如果您需要通过电话或那么这可能容易出错。 https://github.com/salieri/uuid-encoder is a lib that might work for you... https://github.com/salieri/uuid-encoder是一个可能对你有用的库......

Also this means you still have a GUID -you haven't truncated it like the other answers这也意味着你仍然有一个 GUID - 你没有像其他答案那样截断它

The following snippet is a dynamic UUID code.以下片段是动态 UUID 代码。 By default, the legth of the UUID depends on bits but the function randomly chooses characters from the UUID默认情况下,UUID 的长度取决于位,但 function 从 UUID 中随机选择字符

 public String myUUID(int length) {
    String allChars = UUID.randomUUID().toString().replace("-", "");
    Random random = new Random();
    char[] otp = new char[length];
    for (int i = 0; i < length; i++) {
      otp[i] =
          allChars.charAt(random.nextInt(allChars.length()));
    }
    return String.valueOf(otp);
  }

Yes,You can create by using this function.是的,您可以使用此功能创建。

public static String shortUUID() {
  UUID uuid = UUID.randomUUID();
  long l = ByteBuffer.wrap(uuid.toString().getBytes()).getLong();
  return Long.toString(l, Character.MAX_RADIX);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM