原文网址:http://kings11347.blog.163.com/blog/static/56047032201212895416436/
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
public class HashMapUse {
public static void main(String[] args){
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("cn","中国");
hashMap.put("us","米国");
hashMap.put("en","英国");
System.out.println(hashMap);
System.out.println("cn"+hashMap.get("cn"));
System.out.println(hashMap.containsKey("cn"));
System.out.println(hashMap.keySet());
System.out.println(hashMap.isEmpty());
//hashMap.remove("cn");
//System.out.println(hashMap.containsKey("cn"));
//使用Iterator遍历HashMap
Iterator it = hashMap.keySet().iterator();
while(it.hasNext()){
String key = (String) it.next();
System.out.println("key:"+key);
System.out.println("value:"+hashMap.get(key));
}
//使用Entry遍历HashMap
Set<Entry<String, String>> sets = hashMap.entrySet();
for(Entry<String,String> entry : sets){
System.out.println(entry.getKey()+", ");
System.out.println(entry.getValue());
}
}
}
原文:http://www.cnblogs.com/wi100sh/p/4461737.html