随机返回list对象
/** * 返回随机List * @param list 备选 * @param selected 备选数量 * @return */ public List getRandomNum(List list, int selected) { List<Object> reList = new ArrayList<Object>(); Random random = new Random(); // 先抽取,备选数量的个数 if (list.size() >= selected) { for (int i = 0; i < selected; i++) { // 随机数的范围为0-list.size()-1; int target = random.nextInt(list.size()); reList.add(list.get(target)); list.remove(target); } } else { selected = list.size(); for (int i = 0; i < selected; i++) { // 随机数的范围为0-list.size()-1; int target = random.nextInt(list.size()); reList.add(list.get(target)); list.remove(target); } } return reList; }
随机获取部分List<Object>集合
时间: 2024-09-30 07:02:40