Map遍历

前言:在实际开发中常常会遇到需要遍历Map的情况,在搜了部分资料之后,收到了一下两个普遍推荐,且效率较好的遍历方法,如下。

while循环:
  System.out.println("通过Map.entrySet使用iterator遍历key和value:");
  Iterator> it = map.entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry entry = it.next();
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
for循环:
  System.out.println("通过Map.entrySet遍历key和value");
  for (Map.Entry entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }

你可能感兴趣的:(Map遍历)