HashMap集合的遍历方法,使用Iterator迭代器

  • 新建一个HashMap
  • 往HashMap里面存入键值对
  • 通过Iterator遍历HashMap

具体代码如下:

HashMap map = new HashMap();

map.put("one", r.nextInt(10));
map.put("two", r.nextInt(10));
map.put("three", r.nextInt(10));
Iterator iterator = map.entrySet().iterator();
while(iter.hasNext()) {
    Map.Entry entry = (Map.Entry)iterator.next();
    System.out.println("HashMap next : "+ entry.getKey() +" - "+entry.getValue());
}

猜你喜欢

转载自blog.csdn.net/wuzzi/article/details/89313341