1、hashmap遍历方法,首先转为集合Iterator,而后通过hashNext()方法判断,得到next()方法,代码如下:
第一种:效率较高 Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); } 第二种:效率较低 Map map = new HashMap(); Iterator iter = map.keySet().iterator(); while (iter.hasNext()) { Object key = iter.next(); Object val = map.get(key); }