您好,登錄后才能下訂單哦!
@Test
public void t74() {
String[] arrStr = {"2092314", "2092313", "2078103", "588506", "353867", "337220", "337221", "337233", "337189", "318945"};
HashSet<String> getrunmber = getrunmber(1, arrStr);
getrunmber.forEach(m -> Log.info(m));
}
/**
* 根據數組長,返回唯一相應數據
* @param n
* @param str
* @return
*/
public HashSet<String> getrunmber(int n, String[] str) {
Random random = new Random();
HashSet<String> setlist = new HashSet<>();
if (n <= str.length && n != 0 && str.length != 0 && str != null) {
while (setlist.size() < n) {
int c = random.nextInt(str.length);
setlist.add(str[c]);
}
} else {
Log.info("獲取的數據大于String數據的長度");
return null;
}
return setlist;
}
/**
* 通過讀取文件或指定唯一數據
* @param n
* @param str
* @return
*/
public HashSet<String> getrunmber(int n, String str) {
Random random = new Random();
HashSet<String> setlist = new HashSet<>();
ArrayList<String> list = fileReaderr(str);
if (n <= list.size() && n != 0 && list.size() != 0 && str != null) {
while (setlist.size() < n) {
int c = random.nextInt(list.size());
setlist.add(list.get(n));
}
} else {
Log.info("獲取的數據大于String數據的長度");
return null;
}
return setlist;
}
/**
* 通過讀取文件內容
* @param file
* @return list
*/
public static ArrayList<String> fileReaderr(String file) {
BufferedReader bufferedReader = null;
String len = null;
ArrayList<String> list = new ArrayList<>();
File numberpath = new File(file);
if (!numberpath.exists()) {
Log.info("文件不存在");
return null;
}
try {
bufferedReader = new BufferedReader(new FileReader(numberpath));
while ((len = bufferedReader.readLine()) != null) {
list.add(len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。