快捷搜索: 王者荣耀 脱发

Hutool生成UUID方法汇总

Hutool生成UUID方法汇总

参考网址:

下面市hutool工具类的官方参考文档,使用起来很方便

https://www.hutool.cn/docs/#/
RandomUtil.randomInt 获得指定范围内的随机数
RandomUtil.randomBytes 随机bytes
RandomUtil.randomEle 随机获得列表中的元素
RandomUtil.randomEleSet 随机获得列表中的一定量的不重复元素,返回Set
RandomUtil.randomString 获得一个随机的字符串(只包含数字和字符)
RandomUtil.randomNumbers 获得一个只包含数字的字符串
RandomUtil.randomUUID 随机UUID
RandomUtil.weightRandom 权重随机生成器,传入带权重的对象,然后根据权重随机获取对象

说明:

通常我们项目中经常要使用工具类生成UUID

1.字符窜+数字

2.数字

3.随机数字

…等等

我们需要自己上网百度搜索工具类,使用起来很麻烦

解决方案最好的就是使用hutool这个工具类,方便查找,相对还有一定的权威性,因为很多项目都在用

使用示例

/**
 * @Auther: shaoming
 * @Date: 2020/11/23 11:11
 * @Description:
 */
public class UUIDTest {
          
   
    //生成的UUID是带-的字符串,类似于:a5c8a5e8-df2b-4706-bea4-08d0939410e3
    @Test
    public void testUUID(){
          
   
        System.out.println(IdUtil.randomUUID());
    }
    //生成的是不带-的字符串,类似于:b17f24ff026d40949c85a24f4f375d42
    @Test
    public  void testUUIDSimple(){
          
   
        System.out.println(IdUtil.simpleUUID());
    }
    // 获得一个只包含数字的字符串 471222
    //其中参数里面6表示生成随机数的长度
    @Test
    public void testNumUUID(){
          
   
        String s = RandomUtil.randomNumbers(6);
        System.out.println(s);
    }
    //获得一个随机的字符串(只包含数字和字符) tc39lf
    @Test
    public  void testrandomString(){
          
   
        String string = RandomUtil.randomString(6);
        System.out.println(string);
    }
    //获得指定范围内的随机数  96
    @Test
    public  void testrandomInt(){
          
   
        int i = RandomUtil.randomInt(100);
        System.out.println(i);
    }
}
经验分享 程序员 微信小程序 职场和发展