package hanjia;
import java.util.*;
//HahpMap:映射,允许null值和null键,方法是不同步的
public class hanjia{
public static void main(String[] args){
Map map=new HashMap();
Scanner sn=new Scanner(System.in);
String word=null;
System.out.println("输入一年四季的英文单词:");
for(int i=0;i<4;++i) {
System.out.print("输入第"+(i+1)+"个单词:");
word=sn.next();
map.put(new Integer(i), word);//向map中添加元素
}
System.out.println("HashMap的容量:"+map.size()); //返回map的大小
System.out.println(map);
System.out.println("得到指定键所对应的值(1):"+map.get(1));
System.out.println("删除指定键对应的值(元素)(1):"+map.remove(1));
System.out.println("判断map是否包含指定的键(0):"+map.containsKey(0));
System.out.println("判断map是否包含指定的值(1):"+map.containsValue(1));
System.out.println(map);
System.out.println("判断map是否为空:"+map.isEmpty());
System.out.println("得到键集:"+map.keySet());
System.out.println("得到值集:"+map.values());
System.out.println("得到包含映射关系的set图:"+map.entrySet());
map.clear();
System.out.println("清除map里面的内容后:"+map);
}
}
个人理解及资料参考,若有不足或者需要扩展请指点。
若有雷同,纯属巧合。