###########################################################
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class ShuffListMethodSortBoolean {
public static int num = 10000;
public static void main(String[] args) {
List<String> cardList = new ArrayList<>();
for (int i = 0; i < 1000000; i++) {
cardList.add("cardId" + i);
}
long startTime = System.currentTimeMillis();
List<String> randomList = shuffleMethod(cardList);
long endTime = System.currentTimeMillis();
System.out.println("花費時間:" + (endTime - startTime));
// System.out.println(randomList);
}
/**
* 隨機(jī)拿到配置num數(shù)量的數(shù)據(jù)
*/
private static List<String> shuffleMethod(List<String> resultList) {
//防止列表長度比需要的數(shù)量短,按照長度短的決定最終數(shù)量
int randomNum = Math.min(num, resultList.size());
Random random = new Random();
// 索引列表
List<Integer> indexes = new ArrayList<>();
while (indexes.size() < randomNum) {
int index = random.nextInt(resultList.size());
if (!indexes.contains(index)) {
indexes.add(index);
}
}
//對indexes排序
Collections.sort(indexes);
//取出indexes對應(yīng)的list放到newList
List<String> randomList = new ArrayList<>();
for (int index : indexes) {
randomList.add(resultList.get(index));
}
return randomList;
}
}
###########################################################
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
public class ShuffListMethodBitSetListBoolean {
public static void main(String[] args) {
Long start = System.currentTimeMillis();
int num = (int) Math.pow(10, 4);
List<String> cardList = new ArrayList<>();
for (int i = 0; i < Math.pow(10, 6); i++) {
cardList.add("cardId" + i);
}
// 提供一個方法,隨機(jī)從cardList中取出num數(shù)量的元素,并且新列表的元素順序和保持和原列表一致(即:原本在前的還在前邊)
List<String> randomList = shuffleMethod(cardList, num);
// System.out.println(randomList);
Long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
/**
* 隨機(jī)拿到配置num數(shù)量的數(shù)據(jù)
*/
private static List<String> shuffleMethod(List<String> inputList, int num) {
num = num <= inputList.size() ? num : inputList.size();
//存下表
List<Integer> weiZhi = new ArrayList<>();
for (int i = 0; i < inputList.size(); i++) {
weiZhi.add(i);
}
//存獲取的下標(biāo),按照先后順序來
List minList = new ArrayList();
while (minList.size() < num) {
int xiaBiao = (int) (Math.random() * weiZhi.size());
if (!minList.contains(xiaBiao)) {
minList.add(xiaBiao);
}
}
BitSet bit = new BitSet(num);
int size = bit.length();
for (int i = 0; i < num; i++) {
int o = (int) minList.get(i);
bit.set(weiZhi.get(o));
}
List<String> resList = new ArrayList<>();
for (int index = 0; index < inputList.size(); index++) {
if (bit.get(index)) {
resList.add(inputList.get(index));
}
}
return resList;
}
}
###########################################################
import java.util.BitSet;
public class BitSetDemo {
public static void main(String[] args) {
//總長度
int count = 10;
//隨機(jī)取四個
int i = 4;
BitSet bit = new BitSet(count);
while (i > 0) {
//mei有判重
bit.set((int) (Math.random() * count));
i--;
}
for (int index = 0; index < count; index++) {
if (bit.get(index)) {
System.out.print(index + ",");
}
}
}
}