public List<String> randomIntList(){ Random random = new Random(); List<String> list = new ArrayList<String>(); String cur; for (int i = 0; i < 3; i++) { do { cur = String.valueOf(random.nextInt(1000)); }while (list.contains(cur)); list.add(cur); } return list; } public List<String> randomStringList(int len, int size){ List<String> list = new ArrayList<String>(); Integer k; Random random = new Random(); String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < len; i++) { do { for (int a = 0; a < size; a++) { k = random.nextInt(str.length()); stringBuilder.append(str.charAt(k)); } }while (list.contains(stringBuilder)); list.add(stringBuilder.toString()); stringBuilder.delete(0,stringBuilder.length()); } return list; } public void testSort() { List<Student> list = new ArrayList<Student>(); List<String> randomIdList = this.randomIntList(); List<String> randomNameList = this.randomStringList(3,6); for (int i = 0; i < randomIdList.size(); i++) { list.add(new Student(randomIdList.get(i),randomNameList.get(i))); } System.out.println("===========排序前============="); for (Student i:list) { System.out.println("元素:"+i.getId()+":"+i.getName()); } Collections.sort(list); System.out.println("===========排序后============="); for (Student i:list) { System.out.println("元素:"+i.getId()+":"+i.getName()); } }
感谢大佬