在JAVA中生成UUID字符串的有效方法(不帶破折號的UUID.randomUUID()。toString())
最終基于UUID.java實現編寫了自己的東西。請注意,我 ,而是以我能想到的最有效的方式 隨機的32字節十六進制字符串。
實作import java.security.SecureRandom;import java.util.UUID;public class RandomUtil { // Maxim: copied from UUID implementation :) private static volatile SecureRandom numberGenerator = null; private static final long MSB = 0x8000000000000000L; public static String unique() {SecureRandom ng = numberGenerator;if (ng == null) { numberGenerator = ng = new SecureRandom();}return Long.toHexString(MSB | ng.nextLong()) + Long.toHexString(MSB | ng.nextLong()); } }用法
RandomUtil.unique()測驗
我已經測試過一些輸入,以確保其正常工作:
public static void main(String[] args) { System.out.println(UUID.randomUUID().toString()); System.out.println(RandomUtil.unique()); System.out.println(); System.out.println(Long.toHexString(0x8000000000000000L |21)); System.out.println(Long.toBinaryString(0x8000000000000000L |21)); System.out.println(Long.toHexString(Long.MAX_VALUE + 1));}解決方法
我想要一個高效的實用程序來生成唯一的字節序列。UUID是一個很好的候選人,但是會UUID.randomUUID().toString()生成類似的東西44e128a5-ac7a-4c9a-be4c-224b6bf81b20,但是我更喜歡無破折號的字符串。
我正在尋找一種僅從字母數字字符(無破折號或任何其他特殊符號)生成隨機字符串的有效方法。
相關文章:
1. python - django 里自定義的 login 方法,如何使用 login_required()2. python 如何實現PHP替換圖片 鏈接3. mysql - 一個表和多個表是多對多的關系,該怎么設計4. html5 - iOS的webview加載出來的H5網頁,怎么修改html標簽select的樣式字體?5. angular.js - 三大框架react、vue、angular的分析6. 一個mysql聯表查詢的問題7. mysql優化 - mysql count(id)查詢速度如何優化?8. 主從備份 - 跪求mysql 高可用主從方案9. mysql主從 - 請教下mysql 主動-被動模式的雙主配置 和 主從配置在應用上有什么區別?10. javascript - git clone 下來的項目 想在本地運行 npm run install 報錯
