java中的hasmap
最近经常遇到这个问题,可以用for循环,或者用iterator的方法。我更喜欢用for循环,因为当同时处理多个Hashtable时更加灵活。
for循环的方法是:
import java.util.Hashtable;
import java.util.Set;
public class MyHashtableRead {
public static void main(String a[]) {
Hashtable<String, String> hm = new Hashtable<>();
//add key-value pair to Hashtable
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third", "THIRD INSERTED");
System.out.println(hm);
Set<String> keys = hm.keySet();
for (String key : keys) {
System.out.println("Value of " + key + " is: " + hm.get(key));
}
}
}