读书人

取得Map的值的方法

发布时间: 2012-11-08 08:48:11 作者: rapoo

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

读书人网 >编程

热点推荐