在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. android 文件File删除问题2. angular.js - 怎样在使用ng-repeat属性的标签里面监听单个事件,使其能够单个改变CSS。3. css - transform: translateY(-50%)在360浏览器极速模式下使得文字变模糊了4. python - 关于matplotlib的x轴显示的问题5. css - .clearfix:after中为什么设置display: table6. nginx英文文档的WebSocket proxying部分没看太明白,麻烦推荐一点中文文章7. mac连接阿里云docker集群,已经卡了2天了,求问?8. mysql优化 - mysql慢查询copying to tmp table9. mysql多个数据总结成一条数据10. python - django 按日归档统计订单求解
