1 对 set 的遍历 2
3 1.迭代遍历: 4 Set<String> set = new HashSet<String>(); 5 Iterator<String> it = set.iterator(); 6 while (it.hasNext()) { 7 String str = it.next(); 8 System.out.println(str); 9 } 10
11 2.增强or循环遍历: 12 for (String str : set) { 13 System.out.println(str); 14 } 15
16
17 3.优点还体现在泛型 假如 set中存放的是Object 18
19 Set<Object> set = new HashSet<Object>(); 20 for循环遍历: 21 for (Object obj: set) { 22 if(obj instanceof Integer){ 23 int aa= (Integer)obj; 24 }else if(obj instanceof String){ 25 String aa = (String)obj 26 } 27 ........ 28 }