String string = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; Random random = new Random(); ArrayList <String> list = new ArrayList<String>(); for (int i = 0;i<10;){ StringBuffer sb = new StringBuffer(); int length = random.nextInt(10); do{ sb.append(string.charAt(random.nextInt(62))); }while(sb.length()<length); if(!list.contains(sb)){ list.add(sb.toString()); i++; }else{ i=i; } } System.out.println("-------排序之前------"); for(String ss:list){ System.out.println(ss); } System.out.println("---------排序之后---------"); Collections.sort(list); for(String ss :list){ System.out.println(ss); }