java 遍历MAP

//通过取得values()
1.
   public static void work(Map<String, Student> map) {
        Collection<Student> c = map.values();
        Iterator it = c.iterator();
        for (; it.hasNext();) {
            System.out.println(it.next());
        }
    }

 


2.
Set keysSet = map.keySet();
            Iterator iterator = keysSet.iterator();
            while(iterator.hasNext()) {
                Object key = iterator.next();//key
                Object value = map.get(key);//value
            }

 



  //利用keyset进行遍历
  public static void workByKeySet(Map<String, Student> map) {
        Set<String> key = map.keySet();
        for (Iterator it = key.iterator(); it.hasNext();) {
            String s = (String) it.next();
            System.out.println(map.get(s));
        }
    }

  



  //通过entry
public static void workByEntry(Map<String, Student> map) {
        Set<Map.Entry<String, Student>> set = map.entrySet();
        for (Iterator<Map.Entry<String, Student>> it = set.iterator(); it.hasNext();) {
            Map.Entry<String, Student> entry = (Map.Entry<String, Student>) it.next();
            System.out.println(entry.getKey() + "--->" + entry.getValue());
        }
    }
}

 

posted @ 2014-07-08 11:21  共同富裕  阅读(159)  评论(0编辑  收藏  举报