公共靜態void main(String[] args) {
設置& lt字符串& gtstore = getletterandnum(6);
printSet(商店);
}
公共靜態集& lt字符串& gtgetletterandnum(int length) {
設置& lt字符串& gtset = new HashSet & lt字符串& gt();
for(int I = 0;我& lt長度;i++) {
字符串值= get random();
set.add(值);
}
if(set . size()& lt;Length) {//如果沒有生成6比特,
字符串值= get random();//繼續調用生成隨機數的方法。
set.add(值);
}
返回集合;
}
私有靜態字符串get random(){//生成隨機字母和數字方法
字符串值= " ";
Random Random = new Random();
int gen = random . nextint(2);//0、1、2
String charornum = gen % 2 == 0?" char ":" num ";
if ("char "。等於(charornum)) {
int temp = random.nextInt(2) % 2 == 0?65 : 97;
int ascii = random . nextint(26);
value+=(char)(ascii+temp);
} else if ("num "。equalsIgnoreCase(charornum)) {
value+= string . value of(random . nextint(10));
}
返回值;
}
public static void printset(setset){//打印集合的方法。
iterator iterator = set . iterator();
while (iterator.hasNext()) {
String ele =(String)iterator . next();
system . out . print(ele+" ");
}
}