lige
2024-04-18 ed277ece348dae9bc6e36c0fc9f69ae8a3825912
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.ltkj.tduck.utils;
 
import cn.hutool.core.util.RandomUtil;
import lombok.experimental.UtilityClass;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 短Id工具类
 */
@UtilityClass
public class ShortIdUtils {
 
    /**
     * 默认随机字母表,使用URL安全的Base64字符
     */
    private static final char[] DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
 
 
    /**
     * 生成8位长度Id
     *
     * @return
     */
    public String genId() {
//        return NanoId.randomNanoId(null, DEFAULT_ALPHABET, 8);
        return RandomUtil.randomString(8);
    }
 
    public static void main(String[] args) {
        Map<Object, Object> of = new HashMap<>();
        for (int i = 0; i < 100000 ;i++) {
            String s = genId();
            Object put = of.put(s, "1");
            if (null != put) {
                System.out.println(s);
            }
        }
    }
}