JAVA生成短8位UUID的实例讲解

作者:andy_miao858 时间:2021-08-21 04:26:19 

短8位UUID思想其实借鉴微博短域名的生成方式,但是其重复概率过高,而且每次生成4个,需要随即选取一个。

本算法利用62个可打印字符,通过随机生成32位UUID,由于UUID都为十六进制,所以将UUID分成8组,每4个为一组,然后通过模62操作,结果作为索引取出字符,

这样重复率大大降低。

经测试,在生成一千万个数据也没有出现重复,完全满足大部分需求。代码贴出来供大家参考。


public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z" };

public static String generateShortUuid() {
StringBuffer shortBuffer = new StringBuffer();
String uuid = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < 8; i++) {
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
return shortBuffer.toString();
}

补充:生成 8 / 16 / 32 位的UUID

我就废话不多说了,大家还是直接看实例吧~


import java.util.UUID;
public class TestUUID {
// 得到16位的UUID-(数字)
public static String getUUID_16() {
int machineId = 1;// 最大支持1-9个集群机器部署

int hashCodeV = UUID.randomUUID().toString().hashCode();
if (hashCodeV < 0) {// 有可能是负数
hashCodeV = -hashCodeV;
}
String string = machineId + String.format("%015d", hashCodeV);
return string;
}

// 得到32位的UUID-(数字)
public static String getUUID_32() {
return UUID.randomUUID().toString().replace("-", "").toLowerCase();
}

//得到8位的UUID-(码)
public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z" };

public static String getUUID_8() {
StringBuffer shortBuffer = new StringBuffer();
String uuid = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < 8; i++) {
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
return shortBuffer.toString();
}

public static void main(String[] args) {
System.out.println(getUUID_8());
}
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/andy_miao858/article/details/9530245

标签:JAVA,UUID
0
投稿

猜你喜欢

  • 解决Springboot项目启动后自动创建多表关联的数据库与表的方案

    2023-11-24 01:11:27
  • Java中的注解和反射实例详解

    2023-02-02 04:25:45
  • java用接口、多态、继承、类计算三角形和矩形周长及面积的方法

    2021-10-24 22:15:46
  • java抓取网页或文件中的邮箱号码

    2023-07-30 19:19:28
  • Java动态获取实现某个接口下所有的实现类对象集合

    2023-04-01 14:43:20
  • Spring JPA之find拓展方法示例详解

    2021-12-11 03:50:49
  • Spring boot jpa 删除数据和事务管理的问题实例详解

    2022-07-02 12:11:43
  • flutter图片组件核心类源码解析

    2023-09-14 16:29:00
  • Spring Boot之AOP配自定义注解的最佳实践过程

    2023-01-27 17:41:02
  • Spring Security之默认的过滤器链及自定义Filter操作

    2023-11-24 02:48:35
  • Java8函数式接口的基础学习教程

    2023-12-16 12:57:59
  • Java 10 局部变量类型推断浅析

    2023-11-25 06:24:13
  • 深入讲解我们说的CAS自旋锁到底是什么

    2022-05-21 14:43:08
  • Java构建JDBC应用程序的实例操作

    2023-08-07 12:09:13
  • Java代码实现酒店管理系统

    2023-08-13 13:09:23
  • elasticsearch分布式及数据的功能源码分析

    2023-08-11 06:31:26
  • Java面试题冲刺第十六天--消息队列

    2022-08-08 09:07:04
  • Android仿新浪微博分页管理界面(3)

    2023-08-04 19:14:02
  • 完美解决java读取大文件内存溢出的问题

    2023-07-31 17:53:17
  • SpringBoot动态修改日志级别的操作

    2022-10-31 05:00:30
  • asp之家 软件编程 m.aspxhome.com