java 迭代hashmap有几种
2个回答
展开全部
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));
}
2017-09-04
引用jc19861226的回答:
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));}
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));}
展开全部
emails.forEach((k, v)->System.out.println(k +" : "+v);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询