【备忘】Java Map遍历

    //JDK 1.4

    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();
    }

    //JDK 1.5

    Map<String, String> map = new HashMap<String, String>();
    for (Entry<String, String> s : map.entrySet()) {
        System.out.println("键:" + s.getKey() + "\t值:" + s.getValue());
    }

    据说此方法效率高,以后一定要使用此种方式!

你可能感兴趣的:(java,jdk)