Map.entrySet() 这个方法返回的是一个Set<Map.Entry<K,V>>,Map.Entry 是Map中的一个接口,他的用途是表示一个映射项(里面有Key和Value),而Set<Map.Entry<K,V>>表示一个映射项的Set。Map.Entry里有相应的getKey和getValue方法,即JavaBean,让我们能够从一个项中取出Key和Value。
下面是遍历Map的四种方法:

package www.wityx.com;
 public static void main(String[] args) {
 Map<String, String> map = new HashMap<String, String>();
 map.put(“1”, “value1”);
 map.put(“2”, “value2”);
 map.put(“3”, “value3”);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

//第一种:普遍使用,二次取值

System.out.println(“通过Map.keySet遍历key和value:”);
 for (String key : map.keySet()) {
 System.out.println("key= "+ key + " and value= " + map.get(key));
 }
  • 1.
  • 2.
  • 3.
  • 4.

//第二种

System.out.println(“通过Map.entrySet使用iterator遍历key和value:”);
 Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
 while (it.hasNext()) {
 Map.Entry<String, String> entry = it.next();
 System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

//第三种:推荐,尤其是容量大时

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());
 }
  • 1.
  • 2.
  • 3.
  • 4.

//第四种

System.out.println(“通过Map.values()遍历所有的value,但不能遍历key”);
 for (String v : map.values()) {
 System.out.println("value= " + v);
 }
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.