我试图使 java UUID 更短,同时保留与 UUID 相同的唯一性。我写了以下代码:
public static void main(String[] args) {
UUID uid=UUID.randomUUID();
String shortId=to62System(uid.getMostSignificantBits())+
to62System(uid.getLeastSignificantBits());
System.out.println(shortId);
}
static char[] DIGITS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
static int RADIX = DIGITS.length;
public static String to62System(long value) {
if (value == 0) {
return "0";
} else {
char[] buf = new char[11];
int charPos = 10;
long i = value;
while (i != 0) {
buf[charPos--] = DIGITS[Math.abs((int) (i % RADIX))];
i /= RADIX;
}
return new String(buf, charPos + 1, (10 - charPos));
}
}
我做得对还是我忽略了一些重要的事情?