LinkedHashMap源码

news2024/10/7 5:23:05

介绍

LinkedHashMap是HashMap的子类

  • Key和Value都允许空
  • 有序
  • key可重复可覆盖,value可重复
  • 非线程安全
  • 可用于实现LRU
public class LinkedHashMap<K,V>
    extends HashMap<K,V>
    implements Map<K,V>

image-20230611051211473

LinkedHashMap的原理图:

LinkedHashMap是HashMap和双向链表的合二为一,即一个将所有Entry节点链入一个双向链表的HashMap(LinkedHashMap = HashMap + 双向链表)

LinkedHashMap 在不对HashMap做任何改变的基础上,给HashMap的任意两个节点间加了两条连线before指针和after指针,使这些节点形成一个双向链表。在LinkedHashMapMap中,所有put进来的Entry都保存在HashMap中,但由于它又额外定义了一个以head为头结点的空的双向链表,因此对于每次put进来Entry还会将其插入到双向链表的尾部。

在这里插入图片描述

LinkedHashMap和HashMap的Entry结构图:

image-20230611052300456

常量&变量

    //序列化版本号
    private static final long serialVersionUID = 3801124242820219131L;

    /**
     * The head (eldest) of the doubly linked list.
     * 头节点,最老的元素
     */
    transient LinkedHashMap.Entry<K,V> head;

    /**
     * The tail (youngest) of the doubly linked list.
     * 尾节点,最新的元素
     */
    transient LinkedHashMap.Entry<K,V> tail;

    /**
     * The iteration ordering method for this linked hash map: <tt>true</tt>
     * for access-order, <tt>false</tt> for insertion-order.
     * 默认是false,则迭代时输出的顺序是插入节点的顺序。
     * 若为true,则输出的顺序是按照访问节点的顺序。为true时,可以在这基础之上构建LRU
     *
     * @serial
     */
    final boolean accessOrder;

  • header是LinkedHashMap所维护的双向链表的头结点
  • tail是尾节点
  • accessOrder用于决定具体的迭代顺序

构造方法

详情查看hashmap源码

/**
     * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
     * with the specified initial capacity and load factor.
     *
     * @param  initialCapacity the initial capacity
     * @param  loadFactor      the load factor
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive
     */
    public LinkedHashMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
        accessOrder = false;
    }

    /**
     * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
     * with the specified initial capacity and a default load factor (0.75).
     *
     * @param  initialCapacity the initial capacity
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public LinkedHashMap(int initialCapacity) {
        super(initialCapacity);
        accessOrder = false;
    }

    /**
     * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
     * with the default initial capacity (16) and load factor (0.75).
     */
    public LinkedHashMap() {
        super();
        accessOrder = false;
    }

    /**
     * Constructs an insertion-ordered <tt>LinkedHashMap</tt> instance with
     * the same mappings as the specified map.  The <tt>LinkedHashMap</tt>
     * instance is created with a default load factor (0.75) and an initial
     * capacity sufficient to hold the mappings in the specified map.
     *
     * @param  m the map whose mappings are to be placed in this map
     * @throws NullPointerException if the specified map is null
     */
    public LinkedHashMap(Map<? extends K, ? extends V> m) {
        super();
        accessOrder = false;
        putMapEntries(m, false);
    }

    /**
     * Constructs an empty <tt>LinkedHashMap</tt> instance with the
     * specified initial capacity, load factor and ordering mode.
     *
     * @param  initialCapacity the initial capacity
     * @param  loadFactor      the load factor
     * @param  accessOrder     the ordering mode - <tt>true</tt> for
     *         access-order, <tt>false</tt> for insertion-order
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive
     */
    public LinkedHashMap(int initialCapacity,
                         float loadFactor,
                         boolean accessOrder) {
        super(initialCapacity, loadFactor);
        this.accessOrder = accessOrder;
    }

内部类

Entry

实际上是继承自HashMap.Node 静态内部类 ,我们知道HashMap.Node 实际上是一个单链表,因为它只有next 节点,但是这里LinkedHashMap.Entry保留了HashMap的数据结构,同时有before, after 两个节点,一个前驱节点一个后继节点,从而实现了双向链表

    /**
     * HashMap.Node subclass for normal LinkedHashMap entries.
     *  双向链表
     */
    static class Entry<K,V> extends HashMap.Node<K,V> {
        //前后节点
        Entry<K,V> before, after;
        Entry(int hash, K key, V value, Node<K,V> next) {
            super(hash, key, value, next);
        }
    }

LinkedKeySet、LinkedValues 、LinkedEntrySet

**LinkedKeySet:**LinkedHashMap 的 key 集合

**LinkedValues:**LinkedHashMap 的 value 集合

LinkedEntrySet: LinkedHashMap 的 set 集合

结构都类似

    final class LinkedKeySet extends AbstractSet<K> {
        //元素个数
        public final int size()                 { return size; }
        //清空
        public final void clear()               { LinkedHashMap.this.clear(); }
        //key的迭代器
        public final Iterator<K> iterator() {
            return new LinkedKeyIterator();
        }
        //是否包含指定的key
        public final boolean contains(Object o) { return containsKey(o); }
        //通过key移除
        public final boolean remove(Object key) {
            return removeNode(hash(key), key, null, false, true) != null;
        }
        public final Spliterator<K> spliterator()  {
            return Spliterators.spliterator(this, Spliterator.SIZED |
                                            Spliterator.ORDERED |
                                            Spliterator.DISTINCT);
        }
        public final void forEach(Consumer<? super K> action) {
            if (action == null)
                throw new NullPointerException();
            int mc = modCount;
            for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
                action.accept(e.key);
            if (modCount != mc)
                throw new ConcurrentModificationException();
        }
    }

LinkedHashIterator

LinkedHashMap 的元素遍历器

   abstract class LinkedHashIterator {
        //下个Node节点
        LinkedHashMap.Entry<K,V> next;
        //当前节点
        LinkedHashMap.Entry<K,V> current;
        //预期修改次数
        int expectedModCount;

        LinkedHashIterator() {
            next = head;
            expectedModCount = modCount;
            current = null;
        }

        public final boolean hasNext() {
            return next != null;
        }

        final LinkedHashMap.Entry<K,V> nextNode() {
            LinkedHashMap.Entry<K,V> e = next;
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            if (e == null)
                throw new NoSuchElementException();
            current = e;
            next = e.after;
            return e;
        }

        public final void remove() {
            Node<K,V> p = current;
            if (p == null)
                throw new IllegalStateException();
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            current = null;
            K key = p.key;
            removeNode(hash(key), key, null, false, false);
            expectedModCount = modCount;
        }
    }

LinkedKeyIterator、LinkedValueIterator、LinkedEntryIterator

继承了 LinkedHashIterator,只重写 next() 方法

    final class LinkedKeyIterator extends LinkedHashIterator
        implements Iterator<K> {
        public final K next() { return nextNode().getKey(); }
    }

    final class LinkedValueIterator extends LinkedHashIterator
        implements Iterator<V> {
        public final V next() { return nextNode().value; }
    }

    final class LinkedEntryIterator extends LinkedHashIterator
        implements Iterator<Map.Entry<K,V>> {
        public final Map.Entry<K,V> next() { return nextNode(); }
    }

常用方法

newNode

LinkedHashMap并没有重写任何put相关的方法,但其重写了构建节点的newNode方法;newNode方法会在putVal方法中被调用;putVal方法会在向集合中插入数据的时候被调用(单条插入put(K key, V value),批量插入putMapEntries(Map m, boolean evict))。

在每次构建新节点时,通过LinkedNodeLast方法将新节点链接在双向链表的尾部。

    Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
        //创建Entry节点
        LinkedHashMap.Entry<K,V> p =
            new LinkedHashMap.Entry<K,V>(hash, key, value, e);
        //添加到尾部
        linkNodeLast(p);
        return p;
    }

linkNodeLast

    // link at the end of list
    //将指定entry插入到双向链表末尾
    private void linkNodeLast(LinkedHashMap.Entry<K,V> p) {
        LinkedHashMap.Entry<K,V> last = tail;
        //尾指针指向p
        tail = p;
        //如果原尾节点指向null,意味着双向循环链表为空,头尾指针都指向p
        if (last == null)
            head = p;
        else {
            //p的前驱节点指向原尾节点
            p.before = last;
            //原尾节点的后继节点指向p
            last.after = p;
        }
    }

clear

    /**
     * {@inheritDoc}
     * 清空map中元素
     */
    public void clear() {
        // 增加modcount,清除数组内元素
        super.clear();
        // 头等于尾等于null,代表链表为空
        head = tail = null;
    }

containsValue

    /**
     * Returns <tt>true</tt> if this map maps one or more keys to the
     * specified value.
     *
     * @param value value whose presence in this map is to be tested
     * @return <tt>true</tt> if this map maps one or more keys to the
     *         specified value
     *  查看是否包含某个元素
     */
    public boolean containsValue(Object value) {
        //从头到尾遍历双向链表
        for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
            V v = e.value;
            // 直接判断相等或者对象使用equals判断值相等时返回true
            if (v == value || (value != null && value.equals(v)))
                return true;
        }
        return false;
    }

keySet

    /**
     * Returns a {@link Set} view of the keys contained in this map.
     * The set is backed by the map, so changes to the map are
     * reflected in the set, and vice-versa.  If the map is modified
     * while an iteration over the set is in progress (except through
     * the iterator's own <tt>remove</tt> operation), the results of
     * the iteration are undefined.  The set supports element removal,
     * which removes the corresponding mapping from the map, via the
     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
     * operations.
     * Its {@link Spliterator} typically provides faster sequential
     * performance but much poorer parallel performance than that of
     * {@code HashMap}.
     *
     * @return a set view of the keys contained in this map
     * 获取map的key值Set
     */
    public Set<K> keySet() {
        // 获取keySet
        Set<K> ks = keySet;
        if (ks == null) {
            // 实例化LinkedKeySet
            ks = new LinkedKeySet();
            keySet = ks;
        }
        return ks;
    }

values

    /**
     * Returns a {@link Collection} view of the values contained in this map.
     * The collection is backed by the map, so changes to the map are
     * reflected in the collection, and vice-versa.  If the map is
     * modified while an iteration over the collection is in progress
     * (except through the iterator's own <tt>remove</tt> operation),
     * the results of the iteration are undefined.  The collection
     * supports element removal, which removes the corresponding
     * mapping from the map, via the <tt>Iterator.remove</tt>,
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
     * Its {@link Spliterator} typically provides faster sequential
     * performance but much poorer parallel performance than that of
     * {@code HashMap}.
     *
     * @return a view of the values contained in this map
     * 获取map的value值的集合
     */
    public Collection<V> values() {
        // 获取集合元素
        Collection<V> vs = values;
        if (vs == null) {
            // 实例化为LinkedValues
            vs = new LinkedValues();
            values = vs;
        }
        return vs;
    }

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==null ? k==null :
     * key.equals(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) {
        Node<K,V> e;
        // 调用HashMap方法判断元素是否已经存在
        if ((e = getNode(hash(key), key)) == null)
            return null;
        // 当设置accessOrder时,将当前获取的元素放入
        if (accessOrder)
            afterNodeAccess(e);
        return e.value;
    }

afterNodeAccess


    /**
     *       第一种情况,插入节点是头节点
     *       a b c d e
     *       get(a),将a放到末尾
     *       b c d e a
     *       第二种情况,插入节点不是头节点
     *       a b c d e
     *       get(b),将b放到末尾
     *       a c d e b
     *
     *       将元素移动到最后一个
     * @param e
     */
    void afterNodeAccess(Node<K,V> e) { // move node to last
        LinkedHashMap.Entry<K,V> last;
        // 当需要排序并且加入节点e不是尾节点时,进入逻辑(为尾节点时,无需操作,已经为最近访问过的元素)
        if (accessOrder && (last = tail) != e) {
            // 首先暂存插入节点的当前节点,前节点,后节点
            LinkedHashMap.Entry<K,V> p =
                (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
            // 将插入节点的后节点设为null,代表尾节点
            p.after = null;
            // 如果p的前置节点为null,代表插入节点p为头节点,p的后节点a就变为头节点
            // a  ...
            if (b == null)
                head = a;
            else
                // 不为空时, b的后指针指向a节点  b  a
                // b -> a
                b.after = a;
            // 插入节点的后一个节点不为null时,插入节点的后一个节点的前一个节点设置为插入节点的前一个节点  b  p a
            if (a != null)
                //a的上一个节点就变为b
                // b  <- a
                a.before = b;
            else
                //b为尾结点
                last = b;
            // 尾节点为null,代表链表为空,头节点直接设置为插入节点
            if (last == null)
                head = p;
            else {
                // 头节点的前置节点为尾节点
                p.before = last;
                // 尾节点的后一个节点为插入节点
                last.after = p;
            }
            // 新的尾节点设置为插入节点
            tail = p;
            // 增加修改数量
            ++modCount;
        }
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/632826.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Jetpack】Room 中的销毁重建策略 ( 创建临时数据库表 | 拷贝数据库表数据 | 删除旧表 | 临时数据库表重命名 )

文章目录 一、销毁 和 重建策略二、销毁 和 重建策略 核心要点1、创建 Migration 迁移类 - 重点2、配置 Migration 迁移类3、修改数据库版本号4、修改实体类数据类型 三、完整代码示例1、数据库版本 3 代码示例RoomDatabase 数据库类完整代码Student 实体类完整代码执行结果 2、…

chatgpt赋能python:Python编程爱好者必备:如何参加线上比赛

Python编程爱好者必备&#xff1a;如何参加线上比赛 作为一名拥有10年Python编程经验的工程师&#xff0c;我非常喜欢参加线上编程比赛。在这些比赛中&#xff0c;我可以与全球的编程爱好者一起竞技&#xff0c;展现自己编程的优势和挑战自己的能力。在这篇SEO文章中&#xff…

数论与组合数学 期末总结(完结)

数论与组合数学 自然数的基本性质整除最大公约数&#xff08;GCD&#xff09;辗转相除法欧几里得算法 互质 Coprime素数算数基本定理 同余欧拉定理欧拉函数费马小定理威尔逊定理 逆元求逆&#xff1a;欧几里得扩展算法 线性同余方程组 ( a x b m o d m ) (axb\ mod\ m) (axb m…

2023数维杯A题水体污染对流弥散吸附完整论文及所有数值计算模拟代码

大家好呀&#xff0c;从昨天发布赛题一直到现在&#xff0c;总算完成了数维杯A题完整的成品论文。 A题论文37页&#xff0c;一些修改说明提醒7页&#xff0c;正文22页&#xff0c;附录8页 本论文可以保证原创&#xff0c;保证高质量。绝不是随便引用一大堆模型和代码复制粘贴…

chatgpt赋能python:Python如何快速取出所有元素?

Python如何快速取出所有元素&#xff1f; 随着Python编程的流行&#xff0c;我们常常需要处理列表、元组和字典等数据结构。在这些数据结构中&#xff0c;最常见的操作是获取所有元素。本文将介绍如何使用Python来快速取出所有元素。 获取列表中所有元素 在Python中&#xf…

简易操作系统:使用Python 做的图形界面 C 做的内核

目录 实验要求 一、文件管理和用户接口 ⑴文件的逻辑结构 ⑵磁盘模拟 ⑶目录结构 ⑷磁盘分配 ⑸磁盘空闲存储空间管理 ⑹用户接口 ⑺屏幕显示 代码部分 python调用c的方法&#xff1a; ​编辑 c语言部分&#xff0c;文件名 Operating_System_C.c python语言部分 运…

2023认证杯数学建模第二阶段C题完整原创论文讲解

大家好呀&#xff0c;从昨天发布赛题一直到现在&#xff0c;总算完成了认证杯二阶段C题完整的成品论文。 本论文可以保证原创&#xff0c;保证高质量。绝不是随便引用一大堆模型和代码复制粘贴进来完全没有应用糊弄人的垃圾半成品论文。 C第二阶段完整论文共64页&#xff0c;一…

越跌越买!192亿大举抄底

虽然本周主流大盘指数表现乏力&#xff0c;但股票ETF市场再现资金逢低“扫货”。 截至6月9日收盘&#xff0c;全市场736只股票ETF&#xff08;统计股票ETF和跨境ETF&#xff09;总份额增长196亿份&#xff0c;净流入资金约192亿元&#xff0c;再现“越跌越买”的趋势。 其中&…

理解原子操作与CAS锁

理解原子操作与CAS锁实现 线程间内存访问同步的问题理解cpu的存储体系结构cpu存储架构cache line了解一下写回策略 write-back多线程运行在cpu的多核之中&#xff0c;数据怎么共享&#xff0c;怎么同步&#xff1f;通过事件串行化通过MESI 原子操作CAS锁 线程间内存访问同步的问…

chatgpt赋能python:如何去除Python列表中的中括号

如何去除Python列表中的中括号 在Python中&#xff0c;列表是一个非常重要的数据类型。它可以存储多个不同类型的元素&#xff0c;并且可以动态地增加或删除元素。但是&#xff0c;有时候我们需要将列表中的元素取出来&#xff0c;而不想要中括号。本文将介绍两种不同的方法来…

.net framework 命令行项目使用 sqlite,DbContext

文章目录 背景描述实际操作环境安装Nuget包安装三者之间的关系ORM解释 项目从零开始创建过程新建项目安装nugetDB first和Code first新建文件注意&#xff0c;DbContext命名运行测试基础增删改查和原生sql操作查增&#xff0c;删&#xff0c;改增改删 sql语句执行sql查询sql执行…

QFIELD在遥感数据处理中的应用

概述 从卫星图像生成的土地覆盖绘图需要地面实况数据&#xff0c;以便“训练”分类算法&#xff0c;并计算最终地图的准确性。地面实况数据由具有准确位置的数据点和来自已知土地覆盖类型的预定义列表的标签组成。本教程将指导用户完成斐济苏瓦训练区的地面实况数据收集。 收…

chatgpt赋能python:Python反向画圆的方法

Python反向画圆的方法 介绍 Python是一种高级编程语言&#xff0c;在数据科学、机器学习、Web开发等领域具有广泛的应用。其中&#xff0c;Python的图形绘制能力也颇为强大&#xff0c;可以利用Python编写出各种图形和动画效果。本文将介绍Python如何反向画圆&#xff0c;通过…

chatgpt赋能python:Python如何去除空行?

Python如何去除空行&#xff1f; 在Python编程中&#xff0c;经常会遇到需要去除文本文件或字符串中的空行的情况。空行通常是在文件或字符串中不希望存在的&#xff0c;因为它们可能会影响代码的可读性和执行效率。但是&#xff0c;如何才能在Python中高效地去除空行呢&#…

CSS布局模式之Flex布局Grid布局

文章目录 1. 简介1.1 什么是CSS布局&#xff1f;1.2 为什么使用CSS布局&#xff1f; 2. Flex布局2.1 什么是Flex布局&#xff1f;2.2 Flex容器和Flex项目2.3 Flex容器的属性2.3.1 flex-direction2.3.2 justify-content2.3.3 align-items2.3.4 align-content 2.4 Flex项目的属性…

计算机视觉-目标检测(一):从 R-CNN 到 Faster R-CNN

文章目录 1. 概要2. 区域卷积卷积神经网络R-CNN2.1 模型结构2.2 Selective Search2.3 warp2.4 R-CNN训练2.5 R-CNN推理2.6 R-CNN性能评价2.7 R-CNN的缺点 3. SPP-Net3.1 SPP-Net对RCNN的改进3.2 SPP-Net网络结构3.3 SPP-Net训练过程3.4 SPP-Net的问题 4. Fast R-CNN4.1 Fast R-…

浅析设计模式 -- 责任链模式

目录 前言 概述 基本概念 ▐ 结构 ▐ 使用 使用示例 ▐ 代码实现​​​​​​​ ▐ 结果输出 ▐ UML图 扩展 源码赏析 优缺点及适用场景 ▐ 优点 ▐ 缺点 ▐ 适用场景 前言 我们在进行软件开发时要想实现可维护、可扩展&#xff0c;就需要尽量复用代码&…

【数据结构与算法】深入浅出:单链表的实现和应用

&#x1f331;博客主页&#xff1a;青竹雾色间. &#x1f618;博客制作不易欢迎各位&#x1f44d;点赞⭐收藏➕关注 ✨人生如寄&#xff0c;多忧何为 ✨ 目录 前言 单链表的基本概念 节点 头节点 尾节点 单链表的基本操作 创建单链表 头插法&#xff1a; 尾插法&#…

Shell脚本学习记录(常见指令)

Shell echo命令 Shell 的 echo 指令与 PHP 的 echo 指令类似&#xff0c;都是用于字符串的输出。命令格式&#xff1a; echo string 1.显示普通字符串: echo "It is a test" //双引号省略效果相同 2.显示转义字符 echo "\"It is a test\"" …

chatgpt赋能python:Python取出字典中键名对应的值

Python取出字典中键名对应的值 作为一个有10年Python编程经验的工程师&#xff0c;我经常遇到需要从字典中取出键名对应的值的情况。在这篇文章中&#xff0c;我将介绍Python中几种不同的方法来执行这个任务&#xff0c;并提供有关每个方法的优缺点的评估。让我们开始吧&#…