public class Pratice {
public static void main(String[] args) {
Map<String,String> map =new HashMap<String,String>();
map.put("key1","value1");
map.put("key2","value2");
// 使用for循环的keySet
for(String key : map.keySet()){
System.out.println(key +":"+map.get(key));
}
//使用for循环的entryset
for(Map.Entry<String,String> entry:map.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
//使用Iterator迭代 keyset
Iterator<String> iterator = map.keySet().iterator();
while(iterator.hasNext()){
String key = iterator.next();
System.out.println(key+":"+map.get(key));
}
//使用Iteratotr迭代 entryset 注意:iterator不能重复定义,因此下面用iterator1表示;
Iterator<Map.Entry<String,String>> iterator1 = map.entrySet().iterator();
while(iterator1.hasNext()){
Map.Entry<String,String> entry = iterator1.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}