public class MapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("A","测试1");
map.put("B","测试2");
map.put("C","测试3");
map.put("D","测试4");
map.put("E","测试5");
for3Map(map);
}
public static void forMap(Map<String,String> map){
for (Map.Entry<String,String> entry : map.entrySet()){
System.out.println("Key = "+entry.getKey()+"\tValue = "+entry.getValue());
}
}
public static void for2Map(Map<String,String> map){
for(String key : map.keySet()){
System.out.println("Key = " + key);
}
for(String value : map.values()){
System.out.println("value = " + value);
}
}
public static void iteratorMap(Map<String,String> map){
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, String> next = iterator.next();
System.out.println("Key = "+next.getKey()+"\tValue = "+next.getValue());
}
}
public static void for3Map(Map<String,String> map){
for(String key : map.keySet()){
String value = map.get(key);
System.out.println("Key = " + key + "\tValue = " + value);
}
}
}