2021-07-29

这篇博客详细介绍了HashMap的基本操作,包括创建HashMap、put键值对、remove键、get值、containsKey检查键是否存在、keySet获取所有键以及entrySet获取所有键值对。示例代码展示了这些方法的使用,并打印了操作后的结果。
摘要由CSDN通过智能技术生成

hashmap的基本方法

  //创建map对象
        HashMap<Object, Object> map = new HashMap<>();
        map.put(1,12);
        map.put(2,13);
        map.put(3,12);
        System.out.println(map);
        System.out.println("++++++++++++++++++++++++");
  //public V remove(Object key) : 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。
        System.out.println(map.remove(2));
        System.out.println(map);
        System.out.println("+++++++++++++++++++++++++");
 //public V get(Object key) 根据指定的键,在Map集合中获取对应的值。
        System.out.println(map.get(3));
        System.out.println("++++++++++++++++++++++++++");
``
  
``
//boolean containsKey(Object key) 判断集合中是否包含指定的键。
        System.out.println(map.containsKey(2));
        System.out.println(map.containsKey(3));
        System.out.println("++++++++++++++++++++++++++++");
//public Set<K> keySet() : 获取Map集合中所有的键,存储到Set集合中。
        System.out.println(map.keySet());
        System.out.println("+++++++++++++++++++++++++++++");
        System.out.println(map.entrySet());
        System.out.println("++++++++++++++++++++++++++++++");
 //使⽤put⽅法时,若指定的键(key)在集合中没有,则没有这个键对应的值,返回null,并把指定的键值添加到集合中;
        //若指定的键(key)在集合中存在,则返回值为集合中键对应的值(该值为替换前的值),并把指定键所对应的值,替换成指定的新值
        //获取所有的键 获取键集
        Set<Object> key = map.keySet();
        for (Object o : key) {
            //o 就是健值
            //获取对应值
            map.get(o);
            System.out.println(map.get(o));
  // 获取 所有的 entry
Set<Map.Entry<Object, Object>> setentry = map.entrySet();
for (Map.Entry<Object, Object> o : setentry) {
    System.out.println(o.getKey());
    System.out.println(o.getValue());
}`
   结果
{1=12, 2=13, 3=12}
++++++++++++++++++++++++
13
{1=12, 3=12}
+++++++++++++++++++++++++
12
++++++++++++++++++++++++++
false
true
++++++++++++++++++++++++++++
[1, 3]
+++++++++++++++++++++++++++++
[1=12, 3=12]
++++++++++++++++++++++++++++++
12
12
1
12
3
12
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值