Jdk源码解析
HashMap源码部分
1. HashMap 初始化 <源码解析>
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
// 使用默认的初始化容量和默认的加载因子0.75 构造一个空的HashMap,其他所有的字段使用默认值。
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
其他字段指的是如下这些:

/**
* The table, initialized on first use, and resized as
* necessary. When allocated, length is always a power of two.
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
*/
// 第一次使用的时候 table 会初始化,并根据需要调整大小,
// 当分配大小时,长度总是2的次幂,
// 在某些操作中,我们也允许长度为0,为了允许当前不需要的引导机制。
transient Node<K,V>[] table;
/**
* Holds cached entrySet(). Note that AbstractMap fields are used
* for keySet() and values().
*/
// 保存缓的entrySet(),注意点 这个AbstractMap字段被用于keySet()和values()。
// 父类AbstractMap public abstract Set<Entry<K,V>> entrySet();
// -####拓展源码##----》当前类下搜索即可
transient Set<Map.Entry<K,V>> entrySet;
/**
* Returns a {@link Set} view of the mappings 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, or through the
* <tt>setValue</tt> operation on a map entry returned by the
* iterator) 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.
*
// 返回 这个map中包含的一个映射set视图
* @return a set view of the mappings contained in this map
*/
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es;
return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
}
/**
* The number of key-value mappings contained in this map.
*/
// 这个map中包含的key-value的数量
transient int size;
/**
* The number of times this HashMap has been structurally modified
* Structural modifications are those that change the number of mappings in
* the HashMap or otherwise modify its internal structure (e.g.,
* rehash). This field is used to make iterators on Collection-views of
* the HashMap fail-fast. (See ConcurrentModificationException).
*/
// 这个map在结构上被修改的次数,结构修改是指那些改变hashMap的映射数量或
// 其他方式修改其内部结构的修改 例如:rehash。这个字段用于使HashMap集合视图上做迭代器操作的快速失败。
// 参考:ConcurrentModificationException
transient int modCount;
/**
* The next size value at which to resize (capacity * load factor).
* 用于调整大小的下一个size的值 (容量*负载因子)
* @serial
*/
// (The javadoc description is true upon serialization.
// Additionally, if the table array has not been allocated, this
// field holds the initial array capacity, or zero signifying
// DEFAULT_INITIAL_CAPACITY.)
// 如果数组table没有分配,此字段将保留初始数组容量,或使用零标志·表示 DEFAULT_INITIAL_CAPACITY
int threshold;
/**
* The load factor for the hash table.
* hash表的加载因子
* @serial
*/
final float loadFactor;
HashMap源码解析
本文深入解析了HashMap的源码,包括初始化过程、内部结构及关键字段的解释,如table、entrySet、size、modCount等,揭示了HashMap的工作原理。
677

被折叠的 条评论
为什么被折叠?



