当前位置:网站首页>Randomly fetch data from the list
Randomly fetch data from the list
2022-06-13 05:33:00 【zrllllll】
Because I encountered this problem when writing the project , You need to implement this method , I searched the Internet , Found out Collections.shuffle() This method .shuffle Shuffle the cards 、 hold ··· Change position 、 transfer .
// Sort the set randomly
Collections.shuffle(list);
int randomSeriesLength = 5;
// Take out the first five
List<HotUser> randomSeries = list.subList(0, randomSeriesLength);
This is the code example I used , When the last one is returned, you can find list Five pieces of random data in the set .
Next, let's look at the source code .
stay Collections There are two static methods in the class shuffle.
public static void shuffle(List<?> list) {
Random rnd = r;
if (rnd == null)
r = rnd = new Random(); // harmless race.
shuffle(list, rnd);
}
private static Random r;
public static void shuffle(List<?> list, Random rnd) {
//list length
int size = list.size();
// Less than adjustment threshold (SHUFFLE_THRESHOLD = 5) Or support fast random access
//RandomAccess when List Implementations use markup interfaces to indicate that they support fast ( It's usually a constant time ) Random access . The main purpose of this interface is to allow common algorithms to change their behavior , To provide good performance when applied to random or sequential access lists .
if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
for (int i=size; i>1; i--)
// Swap two elements at a specified location
swap(list, i-1, rnd.nextInt(i));
} else {
// If it is greater than the threshold or does not support fast random access, it will be converted to an array for processing
Object arr[] = list.toArray();
// Shuffle array
for (int i=size; i>1; i--)
swap(arr, i-1, rnd.nextInt(i));
// Dump array back into list
// instead of using a raw type here, it's possible to capture
// the wildcard but it will require a call to a supplementary
// private method
// Put the array back into the collection again
ListIterator it = list.listIterator();
for (int i=0; i<arr.length; i++) {
it.next();
it.set(arr[i]);
}
}
}
边栏推荐
- Simple greedy strategy
- 【多线程】线程池核心类-ThreadPoolExecutor
- [thread / multithread] execution sequence of threads
- 动态规划-最长公共子串
- ZABBIX wechat alarm
- Case - random numbers without repetition (HashSet and TreeSet)
- 设置自定义dialog的正确宽高
- Luogu p1012 guess
- Hainan University Postgraduate Entrance Examination electronic information (085400) landing experience
- Std:: Map initialization
猜你喜欢
Install harbor (online offline)
Simple SR: best buddy Gans for highly detailed image super resolution
JS output uincode code
MySQL log management and master-slave replication
List collection concurrent modification exception
Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
Case - grade sorting - TreeSet set storage
About Evaluation Metrics
Use the browser to cut the entire page (take chrome as an example)
Mysql database crud operation
随机推荐
【转载】C语言内存和字符操作函数大全
通过命令行创建harbor镜像库
Fast power code
Wampserver (MySQL) installation
Unity游戏优化(第2版)学习记录7
C language learning log 1.19
How to Algorithm Evaluation Methods
Windbos common CMD (DOS) command set
Case - recursive factorial (recursive)
Bm1z002fj-evk-001 startup evaluation
metaRTC4.0稳定版发布
MySQL main query and sub query
C language learning log 1.16
Detailed explanation of R language sparse matrix
BM1Z002FJ-EVK-001开机测评
External sort
Binary search and binary answer
Pychart encountered time zone problem when connecting to MySQL timezone
ZABBIX wechat alarm
Create a harbor image library from the command line