1、通过数组内容获取想要数组中几个唯一数据
@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;
}