Map接口

Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;
Map中的键值对以Entry类型的对象实例形式存在;
建(key值)不可重复,value值可以重复,一个value值可以和很多key值形成对应关系,每个建最多只能映射到一个值。
Map支持泛型,形式如:Map<K,V>
Map中使用put(K key,V value)方法添加

Map接口中定义的常用方法
具体使用在实现类中讨论

int size();//获取Map集合大小(即元素数量)
boolean isEmpty();//判断是否为空
boolean containsKey(Object key);//判断是否包含某个键
boolean containsValue(Object value);//判断是否包含某个值
V get(Object key);//获取某个键对应的值
V put(K key, V value);//添加键值对(K,V)
V remove(Object key);//移除某个键对应的键值对
void putAll(Map<? extends K, ? extends V> m);//添加另一个Map集合
void clear();//清空所有键值对
Set<K> keySet();//获取键的集合
Collection<V> values();//获取值的集合
Set<Map.Entry<K, V>> entrySet();//获取键值对实体的集合
interface Entry<K,V>//Map中的内部接口

HashMap

基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)除实现了Map接口外还实现了Cloneable,Serializable,继承了AbstractMap抽象类
此类不保证映射的顺序,特别是它不保证该顺序恒久不变。
特点:
键无序,唯一,类似于Set集合
值有序,可重复,类似于List
底层数据结构是哈希表,保证键唯一
允许键为null,值为null

//        HashMap<String, Student> hm = new HashMap<String, Student>();
//        hm.put("2018050401", new Student("2018050401", "张三", 18, 80.0));
//        hm.put("2018050402", new Student("2018050402", "李四", 18, 80.0));
//        hm.put("2018050403", new Student("2018050403", "李四", 18, 80.0));
//        hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0));
//        hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0));
//        
//        // 方式一: 通过键找值
//        Set<String> keys = hm.keySet();
//        for (String key : keys) {
//            Student s = hm.get(key);
//            System.out.println(key + "|" + s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore());
//        }

HashMap<Student, String> hm = new HashMap<Student, String>();
        hm.put(new Student("2018050401", "张三", 18, 80.0),"2018050401");
        hm.put(new Student("2018050402", "李四", 18, 80.0),"2018050402");
        hm.put(new Student("2018050403", "李四", 18, 80.0), "2018050403");
        hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404");
        hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404");
        
        // 方式二: 通过键值对对象找键找值
        Set<Entry<Student, String>> keyValues = hm.entrySet();
        for (Entry<Student, String> keyValue : keyValues) {
            Student s = keyValue.getKey();
            String value = keyValue.getValue();
            System.out.println(s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore() + "=" + value);
        }

LinkedHashMap

Map 接口的哈希表和链表实现,具有可预知的迭代顺序
特点:
键有序,唯一,
值有序,可重复,类似于List
底层数据结构是哈希表和链表,哈希表保证键唯一,链表保证键有序

LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();
        lhm.put(01, "张三1");
        lhm.put(02, "张三2");
        lhm.put(03, "张三3");
        lhm.put(04, "张三4");
        lhm.put(05, "张三5");
        
        Set<Integer> keys = lhm.keySet();
        for (Integer key : keys) {
            System.out.println(key + "|" + lhm.get(key));
        }

TreeMap

基于红黑树(Red-Black tree)的 NavigableMap 实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,
具体取决于使用的构造方法。
特点:
键可排序,唯一,
值有序,可重复,类似于List
底层数据结构是自平衡的二叉树,可排序
排序方式类似于TreeSet,分为自然排序和比较器排序,具体取决于使用的构造方法

TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
        tm.put(24, "Hello1");
        tm.put(14, "Hello2");
        tm.put(34, "Hello3");
        tm.put(124, "Hello4");
        tm.put(24, "Hello5");
        tm.put(24, "Hello6");
        tm.put(24, "Hello7");
        tm.put(244, "Hello8");
        tm.put(624, "Hello9");
        tm.put(24, "Hello10");
        Set<Integer> keys = tm.keySet();
        for (Integer key : keys) {
            String value = tm.get(key);
            System.out.println(key + "|" + value);
        }

HashTable

此类实现一个哈希表,该哈希表将键映射到相应的值。任何非 null 对象都可以用作键或值
特点:
不允许null键和null值
线程安全,效率低
HashMap和Hashtable的区别:

HashMap是不安全的不同步的效率高的  允许null键和null值
Hashtable是安全的同步的效率低的 不允许null键和null值
底层都是哈希表结构

Hashtable<String, String> hashtable = new Hashtable<String, String>();
        hashtable.put("刘备", "孙尚香");
        hashtable.put("孙策", "大乔");
        hashtable.put("周瑜", "小乔");
        hashtable.put("吕布", "貂蝉");
        System.out.println(hashtable);
        Enumeration<String> keys = hashtable.keys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            String value = hashtable.get(key);
            System.out.println(key + "|" + value);
        }

WeakHashMap

以弱键 实现的基于哈希表的 Map。在 WeakHashMap 中,当某个键不再正常使用时,将自动移除其条目。更精确地说,对于一个给定的键,其映射的存在并不阻止垃圾回收器对该键的丢弃,这就使该键成为可终止的,被终止,然后被回收。
丢弃某个键时,其条目从映射中有效地移除,因此,该类的行为与其他的 Map 实现有所不同。

WeakHashMap<String,String> whm = new WeakHashMap<>();
        whm.put(new String("hello1"), "world1");
        whm.put(new String("hello2"), "world2");
        whm.put(new String("hello3"), "world3");
        whm.put("hello4", "world3");
        System.out.println(whm);
        System.gc();
        System.runFinalization();
        System.out.println(whm);

EnumMap

键是枚举类型

EnumMap<Direction, String> em = new EnumMap<>(Direction.class);
        em.put(Direction.UP, "向上移动");
        em.put(Direction.DOWN, "向下移动");
        em.put(Direction.LEFT, "向左移动");
        em.put(Direction.RIGHT, "向右移动");
        
        Set<Direction> keys = em.keySet();
        for (Direction key : keys) {
            String value = em.get(key);
            System.out.println(key + "|" + value);
        }

Map 接口有哪些类的更多相关文章

  1. Java基础 - Map接口的实现类 : HashedMap / LinkedHashMap /TreeMap 的构造/修改/遍历/ 集合视图方法/双向迭代输出

    Map笔记: import java.util.*; /**一:Collection接口的 * Map接口: HashMap(主要实现类) : HashedMap / LinkedHashMap /T ...

  2. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  3. Map接口、HashMap类、LinkedHashSet类

    java.util.Map<K, V>接口 双列集合,key不可以重复 Map方法: 1.public V put(K key, V value):键值对添加到map,如果key不重复返回 ...

  4. Map接口常用实现类学习

    HashMap 1.6的HashMap:数组加单向链表结构 最重要的内部类Entry,全类名是java.util.HashMap.Entry,是个静态类,实现了Map.Entry接口.HashMap. ...

  5. 双列集合Map接口 & Collections工具类

    HashMap 常用方法 遍历方式 iterator迭代器  ITIT HashTable 继承字典 Hashtable--Properties 文件读写 总结 Collections工具类

  6. LinkedHashSet、Map、Map接口HashMap、Hashtable,TreeSet、TreeMap、如何选择使用集合实现类,Collections工具类

    一.Set接口实现类LinkedHashSet 实现继承图: 1.LinkedHashSet的全面说明 1) LinkedHashSet是 HashSet的子类 2) LinkedHashSet底层是 ...

  7. 集合之六:Map接口

    Map接口概述 Map接口中的集合和Collection接口中的集合在存储数据的格式上有很大的不同,Map接口下的内容是以<K , V> ,键值对的形式存储的,我们查询API,Map接口的 ...

  8. Java容器之Map接口

    Map 接口: 1. 实现 Map 接口的类是用来存储键-值(key-value)对: 2. Map 接口的实现类有 HashMap 和 TreeMap 等: 3. Map 类中存储的键-值对,通过键 ...

  9. 自顶向下理解Java集合框架(三)Map接口

    Map基本概念 数据结构中Map是一种重要的形式.Map接口定义的是查询表,或称查找表,其用于储存所谓的键/值对(key-value pair),其中key是映射表的索引. JDK结构中还存在实现Ma ...

随机推荐

  1. 剑指offer-删除链表中重复的结点-链表-python ***

    题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...

  2. 【学习总结】快速上手Linux玩转典型应用-第2章-linux简介

    课程目录链接 快速上手Linux玩转典型应用-目录 目录 1. 什么是Linux 2. Linux能够做什么事情 3. Linux的学习方法 4. 忘掉Windows的所有东西 1. 什么是Linux ...

  3. 锋利的jQuery ——jQuery中的事件和动画(四)

    一.jQuery中的事件 1)加载DOM $(document).ready()和window.onload的区别 1>执行时机 $(document).ready(){}  方法内注册的事件, ...

  4. 3.css3中多个背景图片的用法

    (background-clip裁剪,background-position位置,background-origin定位,background-repeat是否重复) <!DOCTYPE htm ...

  5. UITextView学习笔记

    =================================== UITextView =================================== 1.UITextView常用属性 ...

  6. linux MySql 的主从复制部署

    MySql 复制 mysql 复制:将某一台主机上的 Mysql 数据复制到其它主机(slaves)上,并重新执行一遍从而实现 当前主机上的 mysql 数据与(master)主机上数据保持一致的过程 ...

  7. 关于spring 获取不到网站上的xsd的处理记录

    前两天做一个项目还好好的,今天突然报出这个错误 cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration ...

  8. 数据仓库Hive(一)——hive简介,产生,安装

    1.Hive简介 数据仓库 解释器.编译器.优化器等 运行时,元数据存储在关系型数据库里面 1.1数据库和数据仓库的区别 数据库需要立即返回结果,数据仓库不需要 数据仓库能收纳各种数据源,而数据库只能 ...

  9. 1121. Damn Single (25)

    "Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are suppo ...

  10. Composer\Downloader\TransportException ... Failed to enable crypto,failed to open stream: operation failed

    failed to open stream: operation failed 错误详细信息: [Composer\Downloader\TransportException] The "h ...