import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* @author 作者 weitao:
* @version 创建时间:2016-7-5 下午1:47:00 类说明
*/
public class HashMapTest {
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<Integer, String>();
for(int i=0; i<5; i++){
map.put(i, "test"+i);
}
Set<Integer> keySet = map.keySet();
for (Integer integer : keySet) {
System.out.println(integer+map.get(integer));
}
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
for (Entry<Integer, String> entry : entrySet) {
entry.getKey();
entry.getValue();
System.out.println(entry.getKey() + entry.getValue());
}
Iterator<Integer> itKey = keySet.iterator();
while(itKey.hasNext()){
int key = itKey.next();
String value = map.get(key);
System.out.println(key+value);
}
Iterator<Map.Entry<Integer, String>> itEntrySet = entrySet.iterator();
//建议使用下面方式,遍历起来会快很多
while(itEntrySet.hasNext()){Map.Entry<Integer, String> me = itEntrySet.next();//获取Map.Entry关系对象me
Integer key2 = me.getKey();//通过关系对象获取key
String value2 = me.getValue();//通过关系对象获取value
System.out.println("key: "+key2+"-->value: "+value2);
}
}
}