list 元素随机排序
使用Collections.shuffle()
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UrlMain {
public static void main(String[] args) {
List<Long> longList = new ArrayList<>();
longList.add(111L);
longList.add(112L);
longList.add(113L);
longList.add(114L);
longList.add(115L);
Collections.shuffle(longList);
System.out.println(longList);
}
}
结果:
Collections.shuffle()源码
public static void shuffle(List<?> list) {
Random rnd = r;
if (rnd == null)
r = rnd = new Random(); // harmless race.
shuffle(list, rnd);
}