现在的位置: 首页 > 综合 > 正文

HashMap迭代的几种方式

2013年08月13日 ⁄ 综合 ⁄ 共 720字 ⁄ 字号 评论关闭
  1. private Hashtable<String, String> emails = new Hashtable<String, String>();  
  2.   
  3. //     方法一: 用entrySet()  
  4. //     Iterator it = emails.entrySet().iterator();  
  5. //     while(it.hasNext()){  
  6. //        Map.Entry m=(Map.Entry)it.next();  
  7. //         logger.info("email-" + m.getKey() + ":" + m.getValue());  
  8. //     }  
  9.     
  10.   // 方法二:直接再循环中  
  11.   for (Map.Entry<String, String> m : emails.entrySet()) {  
  12.        logger.info("email-" + m.getKey() + ":" + m.getValue());  
  13.    }  
  14.     
  15.   // 方法三:用keySet()  
  16.    Iterator it = emails.keySet().iterator();  
  17.   while (it.hasNext()){  
  18.        String key;  
  19.        key=(String)it.next();  
  20.        logger.info("email-" + key + ":" + emails.get(key));  
  21.    }

from: http://hi.baidu.com/earth_it/blog/item/833b74092e89a2c73bc763a7.html/cmtid/26b159ed8c89e2d9b21cb16a

抱歉!评论已关闭.