1.迭代遍历:
import java.util.*;
Set<String> keys = new HashSet<String>();
Iterator<String> it = keys.iterator();
for(Iterator it=keys.iterator(); it.hasNext();) {
String str = (String)it.next();
System.out.println(str);
}
2.for循环遍历:
for (String str : keys) {
System.out.println(str);
}
3. 优点还体现在泛型 假如 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
}
........
}
阅读(1448) | 评论(0) | 转发(0) |