/**
* 获取一个1到size连接整数的随机排序集合
* @param size
* @return
*/
public static List getRandomList(int size) {
// 创建list,直接给定初始大小
List list = new ArrayList<>(size);
for (int i = 1;i <= size;i ++) {
list.add(i);
}
List result = new ArrayList<>(size);
// 随机数对象
Random random = new Random();
// 循环size次,随机从list拿出数据,填充到result中
for (int i = 0;i < size;i ++) {
// 获取index,随机数的取值范围为[0,list的长度)
int index = random.nextInt(list.size());
// 拿出并移除指定位置上的元素,追加到result中
result.add(list.remove(index));
}
return result;
}
public static void main(String[] args) {
System.out.println(getRandomList(50));
}