jdk源码剖析之EnumMap

EnumMap的实现原理

    EnumMap底层是通过数组实现的,他是通过Enum的ordinal实现对数组的索引,当我们要使用get方法,实际上是对数组vals[index]做索引操作,而相比于hashMap实际上是先进行hash,然后针对entry做next遍历,因此比较下来EnumMap的get方法的效率相对HashMap等要高一些。

重要方法的实现

    1.get方法

    /**
     * Returns the value to which the specified key is mapped,
     * or {@code null} if this map contains no mapping for the key.
     *
     * <p>More formally, if this map contains a mapping from a key
     * {@code k} to a value {@code v} such that {@code (key == k)},
     * then this method returns {@code v}; otherwise it returns
     * {@code null}.  (There can be at most one such mapping.)
     *
     * <p>A return value of {@code null} does not <i>necessarily</i>
     * indicate that the map contains no mapping for the key; it's also
     * possible that the map explicitly maps the key to {@code null}.
     * The {@link #containsKey containsKey} operation may be used to
     * distinguish these two cases.
     */
    public V get(Object key) {
        return (isValidKey(key) ?
                unmaskNull(vals[((Enum)key).ordinal()]) : null);
    }

    从源码中我们可以看出,get方法是通过对数组取值,时间复杂度为O(1)。

    2.put方法

    /**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for this key, the old
     * value is replaced.
     *
     * @param key the key with which the specified value is to be associated
     * @param value the value to be associated with the specified key
     *
     * @return the previous value associated with specified key, or
     *     <tt>null</tt> if there was no mapping for key.  (A <tt>null</tt>
     *     return can also indicate that the map previously associated
     *     <tt>null</tt> with the specified key.)
     * @throws NullPointerException if the specified key is null
     */
    public V put(K key, V value) {
        typeCheck(key);

        int index = key.ordinal();
        Object oldValue = vals[index];
        vals[index] = maskNull(value);
        if (oldValue == null)
            size++;
        return unmaskNull(oldValue);
    }
    put方法的关键就是key.ordinal(),可以看出EnumMap依赖其key是Enum这一属性,通过Enum的序列取到响应的值。

    3.初始化

    /**
     * Creates an empty enum map with the specified key type.
     *
     * @param keyType the class object of the key type for this enum map
     * @throws NullPointerException if <tt>keyType</tt> is null
     */
    public EnumMap(Class<K> keyType) {
        this.keyType = keyType;
        keyUniverse = getKeyUniverse(keyType);
        vals = new Object[keyUniverse.length];
    }
    EnumMap数组初始化大小为Enum枚举类的大小,不需要增大。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统码+数据库(含vue前端码).zip 【备注】 1、该资内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统码+数据库(含vue前端码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统码+数据库(含vue前端码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统码+数据库(含vue前端码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统码+数据库(含vue前端码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统码+数据库(含vue前端码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统码+数据库(含vue前端码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统码+数据库(含vue前端码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统码+数据库(含vue前端码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统码+数据库(含vue前端码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值