HashMap详解

#一: 集合体系图HashMap存取特点:无序不能重复.HashMap允许一个null值为key和多个null值为value二:HashMap集合的基本操作public class HashMapDemo { public static void main(String[] args) { //声明HashMap对象 Map<String, Integer> map = new HashMap<String, Integer>(); //添加数据
摘要由CSDN通过智能技术生成

#一: 集合体系图
在这里插入图片描述
HashMap存取特点:无序不能重复.
HashMap允许一个null值为key和多个null值为value

二:HashMap集合的基本操作

public class HashMapDemo {
   
	public static void main(String[] args) {
   
		//声明HashMap对象
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		//添加数据
                //map.put(key,value)
		map.put("赵祯", 23) ;
		map.put("老赵", 24) ;
		map.put("老汉", 50) ;
		map.put("老刘", 88) ;
		map.put("Lisi",92);
		//根据键值对键值获取数据
		int value = map.get("赵祯") ;
		System.out.println("数据值为:"+value) ;
		//获取Map中键值对的个数
		int size = map.size() ;
		System.out.println("map中键值对个数为:"+size) ;
		//判断Map集合中是否包含键为key的键值对
		boolean b1 = map.containsKey("赵祯") ;
		boolean b2 = map.containsKey("list") ;
		System.out.println("是否包含键值为赵祯的键值对数据:" + b1) ;
		System.out.println("是否包含键值为list的键值对数据:" + b2) ;
		//判断Map集合中是否包含值为value的键值对
		boolean b3=map.containsValue(23);
		boolean b4=map.containsValue(25);
		System.out.println("是否包含值为23的键值对数据:"+b3);
		System.out.println("是否包含值为24的键值对数据:"+b4);
		//判断Map集合中是否没有任何键值对【是否为空】
		boolean b5=map.isEmpty();
		System.out.println("map中键值对数据是否为空:"+b5);
		//根据键值删除Map中键值对
		int value2=map.remove("Lisi");
		System.out.println("删除了键为Lisi的键值对数据,其值为:"+value2);
		//清空Map集合中所有的键值对
		map.clear();
		boolean b7=map.isEmpty();
		System.out.println
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值