小工具知识网

当前位置: 首页 > 行业知识 > 深入探索Java HashMap底层源码:结构、原理与优化

行业知识

深入探索Java HashMap底层源码:结构、原理与优化

2026-12-04 07:59:17 来源:互联网转载

HashMap是Java集合框架中最常用的数据结构之一,它基于哈希表实现,具备快速的插入、删除和查找操作。本文将详细解析HashMap的底层实现,包括其数据结构、主要方法及其工作原理,并探讨一些常见问题和优化策略。

()
一、HashMap的数据结构

HashMap内部主要通过数组和链表(在Java 8之后引入了红黑树)来存储数据。其基本结构如下:

  1. 数组(table):存储哈希桶(bucket)的数组,数组中的每个元素是一个链表或红黑树的头节点。
  2. 链表:解决哈希冲突的主要方式,多个键值对可能存储在同一个桶中。
  3. 红黑树:当链表长度超过一定阈值(默认为8)时,链表转换为红黑树,以提升性能。
    transient Node[] table; // 存储桶的数组transient int size; // 当前HashMap中的键值对数量transient int modCount; // 结构修改次数int threshold; // 阈值,table扩容的临界点final float loadFactor; // 负载因子
    二、构造函数

    HashMap提供了多个构造函数,常用的有以下两个:

    ()
    public HashMap() {    this.loadFactor = DEFAULT_LOAD_FACTOR; // 默认负载因子0.75}public HashMap(int initialCapacity, float loadFactor) {    if (initialCapacity  MAXIMUM_CAPACITY)        initialCapacity = MAXIMUM_CAPACITY;    if (loadFactor = TREEIFY_THRESHOLD - 1)                        treeifyBin(tab, hash);                    break;                }                if (e.hash == hash &&                    ((k = e.key) == key || (key != null && key.equals(k))))                    break;                p = e;            }        }        if (e != null) {            V oldValue = e.value;            if (!onlyIfAbsent || oldValue == null)                e.value = value;            afterNodeAccess(e);            return oldValue;        }    }    ++modCount;    if (++size > threshold)        resize();    afterNodeInsertion(evict);    return null;}

    putVal方法先计算键的哈希值,然后确定其在数组中的位置。如果该位置为空,直接插入;否则,通过链表或红黑树处理哈希冲突。

  4. get方法
  5. get方法用于根据键查找对应的值:

    public V get(Object key) {    Node e;    return (e = getNode(hash(key), key)) == null ? null : e.value;}final Node getNode(int hash, Object key) {    Node[] tab; Node first, e; int n; K k;    if ((tab = table) != null && (n = tab.length) > 0 &&        (first = tab[(n - 1) & hash]) != null) {        if (first.hash == hash &&             ((k = first.key) == key || (key != null && key.equals(k))))            return first;        if ((e = first.next) != null) {            if (first instanceof TreeNode)                return ((TreeNode)first).getTreeNode(hash, key);            do {                if (e.hash == hash &&                    ((k = e.key) == key || (key != null && key.equals(k))))                    return e;            } while ((e = e.next) != null);        }    }    return null;}

    getNode方法根据键的哈希值定位到数组中的位置,然后在对应的链表或红黑树中查找匹配的节点。

  6. resize方法
  7. resize方法用于扩容数组,当数组中的键值对数量超过阈值时触发:

    final Node[] resize() {    Node[] oldTab = table;    int oldCap = (oldTab == null) ? 0 : oldTab.length;    int oldThr = threshold;    int newCap, newThr = 0;    if (oldCap > 0) {        if (oldCap >= MAXIMUM_CAPACITY) {            threshold = Integer.MAX_VALUE;            return oldTab;        }        else if ((newCap = oldCap = DEFAULT_INITIAL_CAPACITY)            newThr = oldThr  0)        newCap = oldThr;    else {        newCap = DEFAULT_INITIAL_CAPACITY;        newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);    }    if (newThr == 0) {        float ft = (float)newCap * loadFactor;        newThr = (newCap  

    resize方法通过创建一个新的更大的数组,并将旧数组中的元素重新分配到新数组中。旧数组中的元素要么保持在原位置,要么移动到新位置(索引加上旧容量)。

    四、红黑树的引入

    在Java 8中,引入了红黑树以优化链表的性能。当一个桶中的节点数量超过阈值(8个)时,链表将转换为红黑树:

    final void treeifyBin(Node[] tab, int hash) {    int n, index; Node e;    if (tab == null || (n = tab.length)   

上一篇:U盘视频下载指南

下一篇:游戏的服务器怎么找?