在0~List长度中随机取

1
2
3
4
5
6
7
public static void main(String[] args) {

List<String> list = Arrays.asList("a","b","c");
// 随机从0和List长度中获取索引
int index = (int) (Math.random()* list.size());
System.out.println(list.get(index));
}

Collections打乱顺序

1
2
3
4
5
6
7
public static void main(String[] args) {

List<String> list = Arrays.asList("a","b","c");
// shuffle 打乱顺序
Collections.shuffle(list);
System.out.println(list.get(0));
}