首先定义一个Map
Map< String, String> map = new HashMap<String, String>();
map.put(“a”, “a1”);
map.put(“b”, “b1”);
map.put(“c”, “c1”);
//第一种方式:
for (String key : map.keySet()) {
System.out.println(“key = ” + key + ” and value = “+ map.get(key));
}
//第二种方式:
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<java.lang.String, java.lang.String> entry = (Map.Entry<java.lang.String, java.lang.String>) iterator
.next();
System.out.println(“key = ” + entry.getKey() + ” and value = “+ entry.getValue());
}
//第三种方式:推荐,尤其是容量大时
System.out.println(“通过Map.entrySet遍历key和value”);
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(“key= ” + entry.getKey() + ” and value= ” + entry.getValue());
}
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/14937.html