Question:如何随机生成包含N位纯数字的字符串
/**
* 随机生成指定位数的纯数字字符串
* @param count 需要生成的位数
* @return 指定位数的纯数字字符串
*/
public static String getRandomNum(int count) {
StringBuilder sb = new StringBuilder();
String str = "0123456789";
Random r = new Random();
for (int i = 0; i < count; i++) {
// 随机获取0——str.length()之间的整数
int num = r.nextInt(str.length());
// 生成字符串
sb.append(str.charAt(num));
// 去除使用过的数字
str = str.replace((str.charAt(num) + ""), "");
}
return sb.toString();
}
Question:去除List中重复元素
/**
* List集合元素去重
* @param list 需要去重的集合
* @return 去除重复元素后的List集合
*/
private static List removeDuplicate(List list) {
// Set集合不包含重复元素
HashSet h = new HashSet(list);
// 清空原来的list
list.clear();
// 将Set集合置入list
list.addAll(h);
return list;
}