java 遍历 Map

一:常用的
    public static void work(Map map) {
        Collection c = map.values();
        Iterator it = c.iterator();
        for (; it.hasNext();) {
            System.out.println(it.next());
        }
    }
二://利用keyset进行遍历,它的优点在于可以根据你所想要的key值得到你想要的 values,稍具灵活性!!
    public static void workByKeySet(Map map) {
        Set key = map.keySet();
        for (Iterator it = key.iterator(); it.hasNext();) {
            String s = (String) it.next();
            System.out.println(map.get(s));
        }
    }
三:比较复杂的一种遍历,它的灵活性很强
    public static void workByEntry(Map map) {
        Set> set = map.entrySet();
        for (Iterator> it = set.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            System.out.println(entry.getKey() + "--->" + entry.getValue());
        }
    }
}

你可能感兴趣的:(Java,基础)