首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

取得Map的值的方法

2012-11-08 
获得Map的值的方法public class Test {public static void main(String args[]) {// MapString, String

获得Map的值的方法

public class Test {    public static void main(String args[]) {        // Map<String, String> emailsHashtable = new Hashtable<String, String>();        Map<String, String> emails = new HashMap<String, String>();        // 方法一: 用entrySet()        Iterator<Map.Entry<String, String>> its = emails.entrySet().iterator();        while (its.hasNext()) {            Map.Entry<String, String> m = (Map.Entry<String, String>) its.next();            System.out.println("email-" + m.getKey() + ":" + m.getValue());        }        // 方法二: 用entrySet(),直接循环中        for (Map.Entry<String, String> m : emails.entrySet()) {            System.out.println("email-" + m.getKey() + ":" + m.getValue());        }        // 方法三:用keySet()        Iterator<String> it = emails.keySet().iterator();        while (it.hasNext()) {            String key;            key = (String) it.next();            System.out.println("email-" + key + ":" + emails.get(key));        }        // 方法四:change the values of map to collection,and use the Collection.iterator() method.        /*         * Map aa = new Hashtable(); for (Iterator i = aa.values().iterator(); i.hasNext();) { Object temp = i.next(); }         */        Map aa = new HashMap();        for (Iterator i = aa.values().iterator(); i.hasNext();) {            Object temp = i.next();        }    }}
?

热点排行