随机生成字符串-随机生成任意长度的字符串

该博客展示了如何使用Java生成指定长度的随机字符串,并提供了实现不重复数据抽取的方法。通过`RandomStrCreator`类的`createRandomStr`方法生成随机字符串,以及`getDiffNum`方法从预定义数组中抽取不重复的随机数并排序。
摘要由CSDN通过智能技术生成
public class RandomStrCreator {

    private static final String[] tmp = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
            "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };//,"+","/"

    private static Random rand = new Random();

    public static String createRandomStr(int i) {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < i; j++) {
            sb.append(tmp[rand.nextInt(tmp.length)]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        long currentTimeMillis = System.currentTimeMillis();
        String str = createRandomStr(128);
        System.out.println(str);
        System.out.println(System.currentTimeMillis() - currentTimeMillis);

    }

}

结果:

6DAcWg7ilbxbH0WLUWhSScHxSN7AZr065KobhOnB4tIqLNdtgHsA4uzGg91W2jEqfhWn8KjpcuRAHhkSjdSawjkUVefzqCQcTUMBPVe0psnutAHxSh74I1PJiWFkHVxl

0
增加:随机生成不重复数据:

复制代码
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class RandomStrCreator {

    private static final String[] tmp = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
            "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };//,"+","/"

    private static Random rand = new Random();

    public static String createRandomStr(int i) {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < i; j++) {
            sb.append(tmp[rand.nextInt(tmp.length)]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        //Thread t2 = new Thread(() -> System.out.println("use lambda"));
        //HashMap<String, String> map=new HashMap<String, String>();
        /*long currentTimeMillis = System.currentTimeMillis();
        String str = createRandomStr(128);
        System.out.println(str);
        System.out.println(System.currentTimeMillis() - currentTimeMillis);*/
        
        
        List<Integer> diffNum = getDiffNum(6);
        System.out.println(diffNum);
    }

    private static final Integer[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

    /**
     * 
     * #注意:Arrays.asList(arr)方法返回的list,不支持add,move操作 ,会报UnsupportedOperationException的错误
     */
    public static List<Integer> getDiffNum(int i) {
        List<Integer> rs = new ArrayList<>();
        Integer[] numsTmp = nums.clone();
        for (int j = 0; j < i; j++) {
            int len = rand.nextInt(numsTmp.length);
            int tN = numsTmp[len];
            rs.add(tN);
            List<Integer> asList = transferArrayToList(numsTmp);
            asList.remove(len);
            Integer[] intes =new Integer[asList.size()];
            numsTmp = asList.toArray(intes);
        }
        rs.sort((a,b) -> a.compareTo(b));
        return rs;
    }

    
    private static List<Integer> transferArrayToList(Integer[] array){
        List<Integer> transferedList = new ArrayList<>();
        Arrays.stream(array).forEach(arr -> transferedList.add(arr));
        return transferedList;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值