java:HashMap类:实现Map接口,映射,允许null值和null键,方法是不同步的

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);
	}
}

在这里插入图片描述
个人理解及资料参考,若有不足或者需要扩展请指点。
若有雷同,纯属巧合。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值