import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Test1
{
public static void main(String[] args)
{
HashMap maps = new HashMap();
maps.put(“one”, “南京信息工程大学”);
maps.put(“two”, “南京林业大学”);
maps.put(“three”, “南京理工大学”);
//获取单个值
System.out.println(“获取单个值:”+maps.get(“one”));
System.out.println(“—————————-“);
//循环遍历
//1.遍历所有的key,得到key的set集合
Set set=maps.keySet();
Iterator its=set.iterator();
while(its.hasNext())
{
Object key =its.next();
System.out.println(key+”,值为:”+maps.get(key));
}
System.out.println(“—————————-“);
//2.遍历得到key和value的内部对象Entry
Set entrys=maps.entrySet();
Iterator its1=entrys.iterator();
while(its1.hasNext())
{
// HashMap.Entry 实际是key,value的一个对象
HashMap.Entry entry=(HashMap.Entry)its1.next();
System.out.println(entry.getKey()+”,”+entry.getValue());
}
System.out.println(“—————————-“);
//3.获取值 局限就是只能获取值 ,不能得到key
Collection c=maps.values();
Iterator its2=c.iterator();
while(its2.hasNext())
{
System.out.println(its2.next());
}
//4.泛型遍历
System.out.println(“—————————-“);
Map
maps1.put(“aa”, “南京”);
maps1.put(“bb”, “北京”);
maps1.put(“cc”, “上海”);
for(String s:maps1.keySet())
{
System.out.println(s+”,”+maps1.get(s));
}
}
}<br />