久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

JAVA生成短8位UUID的實例講解

瀏覽:8日期:2022-08-18 14:28:27

短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());} }

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Java
相關文章:
主站蜘蛛池模板: 日韩中文字幕一区二区 | 国产美女一区 | 欧美成人免费在线视频 | 国产高清一区二区 | 日韩视频网 | 午夜视频免费 | 亚洲综合精品 | 欧美日韩国产精品 | 日韩视频一区二区三区 | 久久久久久久久久久久91 | 一区二区三区在线 | 欧 | 国产日韩欧美91 | 久久久久久久久99精品 | 美女视频一区 | 久久精品二区亚洲w码 | 成人精品视频一区二区三区 | 久久午夜电影院 | 欧美精品二区 | 成人涩涩网站 | 国产伦一区 | 天天干国产 | 亚洲欧美少妇 | 日本中文字幕视频 | 91精品国产综合久久久久久 | 秋霞av电影| 国产精品视频导航 | 色婷婷一区二区三区四区 | 91成人免费在线观看 | 欧美成年黄网站色视频 | 国内精品视频一区国产 | 视频一区久久 | 国产一区二区视频在线观看 | 国产目拍亚洲精品99久久精品 | 亚洲看片| 国产精品毛片在线 | 日韩中文字幕在线观看 | 国产九九精品 | 国产91网址 | 久久欧美精品一区 | 人人鲁人人莫一区二区三区 | 亚洲日韩成人 |