package test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class OverloadTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,Integer> maps = new HashMap<String,Integer>();
maps.put("第一个", 1);
maps.put("第二个 ",2);
maps.put("第三个", 3);
Set<Entry<String,Integer>> entrySet = maps.entrySet();//第一种方法 得到含有Entry类型的Set
Iterator iterator = entrySet.iterator();
while(iterator.hasNext()){
Entry entry = (Entry) iterator.next();//Entry 中包含map的键和值
System.out.println(entry.getKey());//得到键
System.out.println(entry.getValue());得到值
}
System.out.println("=========");
Set<String> keySet = maps.keySet();//第二种方法 得到含有key的Set
Iterator iterator1 = keySet.iterator();
while(iterator1.hasNext()){
String key = (String)iterator1.next();
System.out.println(key);
System.out.println(maps.get(key));
}
}
}
|
|