> 对比 HashMap 《HashMap1.8》

一、概述

image.png
HashTable 特点

  • 底层:数组+ 链表
  • 线程安全(通过 synchronized 修饰方法)
  • 不允许 null 键 和 null 值

    二、代码分析

    2.1、属性

    image.png
    如下:

  • Entry<?,?>[] table
    数组

  • int count;
    键值对个数
  • int threshold
    扩容阈值
  • float loadFactor;
    负载因子
  • modCount
    Map修改变更次数

    2.2、构造函数

    image.png
    根据无参构造得出结论:

  • 默认初始容器大小:11

  • 负载因子默认:0.75f

    2.3、获取(get)

    image.png
    根据上述代码得出结论:

  • 方法线程安全(加了 synchronized 关键字)

  • 通过链表解决 hash 冲突
  • 使用 % 计算索引值(比较 HashMap 异或 消息低)

    2.4、设置(put)

    image.png
    根据上述代码得出结论:

  • key、value 不能为空

  • 线程安全(使用 synchronized 修饰)

    2.5、扩容

    image.png
    扩容范围为:原来容器大小的2倍 +1