Java中List集合中随机取特定个数的子项
1 public List<TSDepart> getSubStringByRadom(List<TSDepart> list, int count){ 2 List backList = null; 3 backList = new ArrayList<TSDepart>(); 4 Random random = new Random(); 5 int backSum = 0; 6 if (list.size() >= count) { 7 backSum = count; 8 }else { 9 backSum = list.size(); 10 } 11 for (int i = 0; i < backSum; i++) { 12 // 随机数的范围为0-list.size()-1 13 int target = random.nextInt(list.size()); 14 backList.add(list.get(target)); 15 list.remove(target); 16 } 17 return backList; 18 }