当前位置:   article > 正文

【数据结构】Map的常用方法

【数据结构】Map的常用方法


一、搜索

1.概念

  • 1.Map和Set是一种专门用来进行搜索的容器/数据结构,搜索的效率和具体实例化的子类有关。

    分别实例化TreeMap、HashMap,两者存在效率上的差别。

            Map<String,Integer>map1 =new TreeMap<>();//查找的复杂度为o(logN)
            Map<String,Integer>map2 =new HashMap<>();//查找的复杂度为o(1)
            // TreeMap的底层是搜索树
            // HashMap的底层是哈希表:数组+链表+红黑树组成
    
    • 1
    • 2
    • 3
    • 4
  • 2.常见的搜索方法为 静态查找,一般不进行插入、删除。

    1,直接遍历: 时间复杂度为 o(N),元素越多,效率越慢。

    2.二分查找:时间复杂度为 o(log2N),要求搜索前必须是有序的。

  • 3.而Map和Set是可以进行 动态查找 的集合容器。

  • 4.只有关键字的叫纯Key模型(Set),由关键字和其对应的值形成的键值对,叫Key-Value模型(Map)

二、Map的使用

1.概念:

在这里插入图片描述

  • Map是一个接口类,没有继承于Collection。
  • 存储的形式是 Key-Value 键值对,并且Key是唯一的,不能重复。

2.Map的常用方法:

1.V put(K Key ,V Value )

设置Key对应的Value,将元素对应的值进行存储

在这里插入图片描述

  • Map的底层是一棵搜索树,在放进搜索树的时候要进行比较,比较的对象是关键字Key
    public static void main(String[] args) {
        Map<String,Integer>map1 =new TreeMap<>();//查找的复杂度为o(logN)
        map1.put("Math",3);
        map1.put("Chinese",2);
        map1.put("English",4);
        System.out.println(map1);
    }
	//{Chinese=2, English=4, Math=3}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
2.V get(Object key)
  • 返回对应的value,通过Key得到对应的值
        System.out.println(map1.get("Math"));//2
        System.out.println(map1.get("Art"));//null
  • 1
  • 2

如果get传进的Key不存在,则返回的Value值为null

3.V getOrDefault(Object key, V defaultValue)
  • 返回 key 对应的 value,key 不存在,返回默认值

             map1.put("Math",3);
    		System.out.println(map1.getOrDefault("Science", 7));//7
             System.out.println(map1.getOrDefault("Math", 7));   //3
    
    • 1
    • 2
    • 3

    如果给的Key值在Map中存在,返回原本的Value,忽视自己填的默认值

4.V remove(Object key)
  • 根据Key来删除K-V的映射关系
        System.out.println(map1.remove("English"));//4
        System.out.println("++++++");
        System.out.println(map1.containsValue(4));		//false
        System.out.println(map1.containsKey("English"));//false
  • 1
  • 2
  • 3
  • 4

再remove()中传入Key, key和其对应的Value均删除

remove()返回删除的Value

5.Set keySet()
  • 返回所有 key 的不重复集合 ,返回的是一个set
        Set<String>set = map1.keySet();
        System.out.println(set);//[Chinese, English, Math]
  • 1
  • 2
6.Collection values()
  • 返回所有 value 的可重复集合
        Collection<Integer> values = map1.values();
        System.out.println(values);//[2, 4, 3]
  • 1
  • 2
7.Set<Map.Entry<K, V>> entrySet()
  • 返回所有的 key-value 映射关系

  • 将map的Key和Value看作一个整体(Entry),Set中存放不同的Entry

        Set<Map.Entry<String, Integer>> entrySet = map1.entrySet();
        for (Map.Entry<String, Integer> entry : entrySet) {
            System.out.println("Key:"+entry.getKey()+" Value:"+entry.getValue());
        }

//Key:Chinese Value:2
//Key:English Value:4
//Key:Math Value:3

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
8.boolean containsKey(Object key)
  • 判断是否包含 key
        System.out.println(map1.containsKey("Chinese"));//true
  • 1
9.boolean containsValue(Object value)
  • 判断是否包含Value
        System.out.println(map1.containsValue(3));true
  • 1

3.Map的注意事项

  • 1.Map是一个接口,不能直接实例化对象。只能实例化它的实现类TreeMap和HashMap
  • 2.Map中存放键值对中的Key是唯一的,Value不唯一
        map1.put("English", 4);
        map1.put("English",44);
  • 1
  • 2

如果存储相同的Key,会覆盖掉之前的Value.

  • 3.在TreeMap中插入键值对时,Key不能为空,否则会抛出空指针异常,Value可以为空。HashMap的Key和Value都可以为空
  • 4.Map中的Key因为不能重复,可以全部用keySet分离出来,存进Set中。Value也可以分离出来,存在Collection的任何一个子集合中(可以重复)
  • 5.Map中的键值对,Key不能直接修改,Value可以直接修改。如皋要修改Key,需要先删除Key,再重新插入

点击移步博客主页,欢迎光临~

偷cyk的图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/953922
推荐阅读
相关标签
  

闽ICP备14008679号