參考: https://www.cnblogs.com/magicya/p/6683052.html
對 set 的遍歷
1.迭代遍歷:
Set<String> set = new HashSet<String>();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String str = it.next();
System.out.println(str);
}
2.for循環遍歷:
for (String str : set) {
System.out.println(str);
}
優點還體現在泛型 假如 set中存放的是Object
Set<Object> set = new HashSet<Object>();
for循環遍歷:
for (Object obj: set) {
if(obj instanceof Integer){
int aa= (Integer)obj;
}else if(obj instanceof String){
String aa = (String)obj
}
........
}
hashSet的遍歷
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.