在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. dockerfile - [docker build image失败- npm install]2. java - mybatis怎么实现在数据库中有就修改,没有就添加3. docker安装后出现Cannot connect to the Docker daemon.4. docker gitlab 如何git clone?5. javascript - webpack 多入口文件生成HTML文件;6. node.js - mongoDB使用$gte的问题7. nignx - docker内nginx 80端口被占用8. java中关于直接插入排序遇到的问题。9. javascript - npm run build后调用api返回index.html10. docker绑定了nginx端口 外部访问不到
