hashMap常用方法

这篇博客详细介绍了HashMap的各种操作,包括插入值、获取值、检查空、查找键和值、删除键值对、显示键和值、计数、添加Map、替换值、清空和克隆HashMap。还特别提到了compute方法在更新value时的应用。
摘要由CSDN通过智能技术生成

hashMap放入值

public static void main(String[] args) {
         ///*Integer*/map.put("1", 1);//向map中添加值(返回这个key以前的值,如果没有返回null)
         HashMap<String, Integer> map=new HashMap<>();
         System.out.println(map.put("1", 1));//null
         System.out.println(map.put("1", 2));//1
     }

hashMap取值

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        map.put("DEMO", 1);
        /*Value的类型*///得到map中key相对应的value的值
        System.out.println(map.get("1"));//null
        System.out.println(map.get("DEMO"));//1
    }

hashMap判断是否为空

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///判断map是否为空
        System.out.println(map.isEmpty());//true
        map.put("DEMO", 1);
        System.out.println(map.isEmpty());//false
    }

hashMap判断是否有key

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///判断map中是否存在这个key
        System.out.println(map.containsKey("DEMO"));//false
        map.put("DEMO", 1);
        System.out.println(map.containsKey("DEMO"));//true
    }

hashMap判断是否有value

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值