Map(1)--Map集合的常用功能

Map集合的常用功能:

  • a:添加功能
    * V put(K key,V value):添加元素。
    * 如果键是第一次存储,就直接存储元素,返回null
    * 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
  • b:删除功能
    * void clear():移除所有的键值对元素
    * V remove(Object key):根据键删除键值对元素,并把值返回
  • c:判断功能
    * boolean containsKey(Object key):判断集合是否包含指定的键
    * boolean containsValue(Object value):判断集合是否包含指定的值
    * boolean isEmpty():判断集合是否为空
  • d:获取功能
    * Set《Map.Entry《K,V》》 entrySet(): //英文状态下的三角号不显示,用书名号替代了
    * V get(Object key):根据键获取值
    * Set《K》 keySet():获取集合中所有键的集合
    * Collection《v》 values():获取集合中所有值的集合
  • e:长度功能
    * int size():返回集合中的键值对的个数

public static void main(String[] args) {
// demo1();
// demo2();
// demo3();
// demo4();
}

public static void demo1() {
        Map<String, Integer> map=new HashMap<String, Integer>();
        map.put("张三", 23);
        Integer i1=map.put("张三", 23);
        map.put("李四", 24);
        map.put("王五", 25);
        map.put("赵六", 26);

        System.out.println(map);
        System.out.println(i1);             //相同的键不存储,把覆盖的键值value返回
    }

    public static void demo2() {
    Map<String, Integer> map=new HashMap<String, Integer>();
    map.put("张三", 23);
    map.put("李四", 24);
    map.put("王五", 25);
    map.put("赵六", 26);

    Integer value=map.remove("张三");
    System.out.println(value);          //23
    System.out.println(map);            //{赵六=26, 李四=24, 王五=25}
}
    public static void demo3() {
    Map<String, Integer> map=new HashMap<String, Integer>();
    map.put("张三", 23);
    map.put("李四", 24);
    map.put("王五", 25);
    map.put("赵六", 26);

    System.out.println(map.containsKey("张三"));  //true
    System.out.println(map.containsValue(23));  //true
    System.out.println(map);            //{赵六=26, 张三=23, 李四=24, 王五=25}
}
public static void demo4() {
    Map<String, Integer> map=new HashMap<String, Integer>();
    map.put("张三", 23);
    map.put("李四", 24);
    map.put("王五", 25);
    map.put("赵六", 26);

    Collection<Integer> c=map.values();
    System.out.println(c);              //获取集合中所有值的集合    [26, 23, 24, 25]
    System.out.println(map.size());     //4
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值