JDK(八)JDK1.7&1.8源码对比分析【集合】HashMap

JDK1.8源码分析【集合】HashMap文章中,我们分析了HashMap在JDK1.8中新增的特性(引进了红黑树数据结构),但是为什么要进行这个优化呢?这篇文章我们通过对比JDK1.7和1.8来分析优化的原因。

众所周知,HashMap底层是基于 数组 + 链表 的方式实现的,不过在JDK1.7和1.8中具体实现稍有不同。

目录

一、对比分析

1. 1.7版本

2. 1.8版本

总结

一、对比分析 1. 1.7版本

1.7 中的数据结构图:

JDK(八)JDK1.7&1.8源码对比分析【集合】HashMap

先来看看1.7中几个比较核心的成员变量:

/** * The default initial capacity - MUST be a power of two. * 初始桶大小,因为底层是数组,所以这是数组的大小 */ static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 /** * The maximum capacity, used if a higher value is implicitly specified * by either of the constructors with arguments. * MUST be a power of two <= 1<<30. * 桶最大值 */ static final int MAXIMUM_CAPACITY = 1 << 30; /** * The load factor used when none specified in constructor. * 默认的负载因子 */ static final float DEFAULT_LOAD_FACTOR = 0.75f; /** * An empty table instance to share when the table is not inflated. */ static final Entry<?,?>[] EMPTY_TABLE = {}; /** * The table, resized as necessary. Length MUST Always be a power of two. * 真正存放数据的数组 */ transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE; /** * The number of key-value mappings contained in this map. * Map存放数量的大小 */ transient int size; /** * The next size value at which to resize (capacity * load factor). * 桶大小,可在初始化时显式指定 * @serial */ // If table == EMPTY_TABLE then this is the initial capacity at which the // table will be created when inflated. int threshold; /** * The load factor for the hash table. * 负载因子,可在初始化时显式指定 * * @serial */ final float loadFactor;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzygsy.html