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


    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;
        }
    }
  • 相关阅读:
    问题14:如何拆分含有多种分隔符的字符串
    问题15:如何判断字符串a是否以字符串b开头或结尾
    问题16:如何调整字符串中文本的格式
    第三方支付公司之快钱
    js实现回调功能实例
    oracle查看未提交事务
    Tomcat错误之java.lang.OutOfMemoryError:PermGen space解决方案
    oracle错误之未知的命令开头imp忽略了剩余行解决方案
    修改easyui日期控件只显示年月,并且只能选择年月
    数据库三范式大总结
  • 原文地址:https://www.cnblogs.com/zyf-yxm/p/13497187.html
Copyright © 2020-2023  润新知