获取随机key

Posted light-zhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取随机key相关的知识,希望对你有一定的参考价值。

import java.util.Set;

import io.netty.util.internal.ConcurrentSet;

/**
 * 获取key
 * 
 * @project common-utils
 * @fileName GetKey.java
 * @Description
 * @author light-zhang
 * @date 2018-10-12 17:10:33
 * @version 1.0.0
 */
public final class GetKey {

    private static final String baseDigits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
    private static final int BASE = baseDigits.length();
    private static final char[] digitsChar = baseDigits.toCharArray();
    private static final int FAST_SIZE = ‘z‘;
    private static final int[] digitsIndex = new int[FAST_SIZE + 1];

    static {
        for (int i = 0; i < FAST_SIZE; i++) {
            digitsIndex[i] = -1;
        }
        for (int i = 0; i < BASE; i++) {
            digitsIndex[digitsChar[i]] = i;
        }
    }

    /**
     * 参数解码
     * 
     * @param paramter
     * @return
     */
    public static long decode(String paramter) {
        long result = 0L;
        synchronized (paramter) {
            long multiplier = 1;
            for (int pos = paramter.length() - 1; pos >= 0; pos--) {
                result += getIndex(paramter, pos) * multiplier;
                multiplier *= BASE;
            }
        }
        return result;
    }

    /**
     * 传入系统纳秒,防止重复
     * 
     * @param number
     * @return
     */
    public static String encode(Long number) {
        final StringBuilder builder = new StringBuilder();
        synchronized (number) {
            if (number < 0)
                new IllegalArgumentException("Number(Base64) must be positive: ".concat(number.toString()));
            if (number == 0)
                return "0";

            while (number != 0) {
                builder.append(digitsChar[(int) (number % BASE)]);
                number /= BASE;
            }
        }
        return builder.toString();
    }

    private static int getIndex(String paramter, int pmindex) {
        char c = paramter.charAt(pmindex);
        if (c > FAST_SIZE) {
            new IllegalArgumentException("Unknow character for Base64: ".concat(paramter));
        }
        int index = digitsIndex[c];
        if (index == -1) {
            new IllegalArgumentException("Unknow character for Base64: ".concat(paramter));
        }
        return index;
    }

    public static void main(String[] args) {
        Set<String> data = new ConcurrentSet<String>();
        for (int i = 0; i < 20000000; i++) {
            // System.out.println(encode(System.nanoTime()));
            data.add(encode(System.nanoTime()));
            // System.out.println(decode(UUID.randomUUID().toString()));
        }
        System.out.println(data.size());
        // System.out.println(decode("6c7P6dzh"));
    }
}

 

以上是关于获取随机key的主要内容,如果未能解决你的问题,请参考以下文章

获取随机key

随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value

Python snippet(代码片段)

播放随机声音而不重复

如何从列表中获取随机字符串值 - android [关闭]

js获取url的参数