举出几种 HashMap 的迭代方式

来源:互联网 发布:常见网络诈骗手段有 编辑:程序博客网 时间:2024/05/21 19:56
HashMap<String, String> emails = new HashMap<String, String>(); //方法一: 用entrySet()  Iterator it = emails.entrySet().iterator();  while(it.hasNext()){ Map.Entry m=(Map.Entry)it.next(); System.out.println("email-" + m.getKey() + ":" + m.getValue());  }  // 方法二:直接再循环中  for (Map.Entry<String, String> m : emails.entrySet()) {  System.out.println("email-" + m.getKey() + ":" + m.getValue()); }  // 方法三:用keySet()  Iterator it = emails.keySet().iterator();  while (it.hasNext()){ String key=(String)it.next(); System.out.println("email-" + key + ":" + emails.get(key));
原创粉丝点击