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();
}
}
}
获得Map的值的方法
最新推荐文章于 2022-11-13 22:30:16 发布