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);  
}  

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  
   }  
   ........  
}   


免責聲明!

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



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