Java中遍歷Set集合的方法


 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM