import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class Map {
public static void main(String[] args) {
HashMap<String,String> m1 = new HashMap<String,String>();
m1.put("key1", "value1");
m1.put("key2", "value2");
m1.put("key3", "value3");
System.out.println("");
System.out.print(m1);
System.out.println();
//Map取值的方法之一: foreach 方法
System.out.println("Method 1:");
for(String key : m1.keySet()) {
System.out.println("key="+ key + " and value is : " + m1.get(key));
}
//Map取值方式之二:Iterator构造操作符来读取
Iterator<Entry<String, String>> it = m1.entrySet().iterator();
System.out.println("Method 2:");
while(it.hasNext()) {
Entry<String, String> entry = it.next();
System.out.println("key is : " + entry.getKey() + " and value is : "+ entry.getValue());
}
//Map取值方式之三: Entry and entrySet()
System.out.println("Method 3:");
for(Entry<String, String> entry : m1.entrySet()) {
System.out.println("value is : " + entry.getKey() + " and value is : " + entry.getValue());
}
//Map取值方式之四: 通过Map.values遍历(只能获取values)
System.out.println("Mehod 4:");
for(String v : m1.values()) {
System.out.println("value is: "+ v);
}
}
}
打开App,阅读手记