package Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
public class Map {
/**
* @param args
*/
public static void main(String[] args) {
//先设置好一个map
HashMap<String,Object> map= new HashMap<String,Object>();
map.put( "1" , "1" );
map.put( "2" , "2" );
map.put( "3" , "3" );
map.put( "4" , "4" );
map.put( "5" , "5" );
//使用keySet()从一个map中得到所有的信息
Set<String> keys=map.keySet();
if (keys!= null )
//得到迭代器
{
Iterator<String> it=keys.iterator();
while (it.hasNext())
{
String key=it.next();
String value=map.get(key).toString();
System.out.println(value);
}
}
//使用entrySet()从一个map中得到所有信息
Set<Entry<String,Object>> s=map.entrySet();
if (s!= null ){
Iterator<Entry<String,Object>> ite=s.iterator();
while (ite.hasNext()){
Entry<String,Object> en=ite.next();
String key=en.getKey();
String value=en.getValue().toString();
System.out.println( "key:" +key+ " " + "value:" +value);
}
}
}
}
|