闲来没事,练练基础,呵呵。。。,记得当初面试的时候经常有人问map怎么遍历的问题,感觉被问到的概率还是蛮高的,眼过十遍不如手过一遍啊,写一个
class="java"> public static void main(String[] args) { Map<Object, Object> map = new HashMap<Object, Object>(); map.put("name","张三丰" ); map.put("age","32" ); map.put("address","北京.朝阳"); /** * 方法一 */ Iterator<Entry<Object, Object>> it=map.entrySet().iterator(); while(it.hasNext()){ Entry<Object, Object> m= it.next(); System.out.println(m.getKey()+":"+m.getValue()); } /** * 方法二 */ Iterator<Object> it1 =map.keySet().iterator(); while(it1.hasNext()){ String key =it1.next().toString(); String value =map.get(key).toString(); System.out.println(key+":"+value); } /** * 遍历获取map中的值 */ Iterator<Object> it2 =map.values().iterator(); while(it2.hasNext()){ System.out.println(it2.next()); } }
?
?