java_集合学习(一)

本文详细介绍了Java中HashMap的实现原理,包括其基于Map接口的特性、允许null键值、非同步特性以及不保证有序等关键信息。探讨了HashMap的容量与负载因子的概念,分析了构造函数如何初始化数组,并解释了Entry结构如何构成链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java
HashMap
⼀、概述
在官⽅⽂档中是这样描述HashMap的:
Hash table based implementation of the Map interface. This implementation provides
all of the optional map operations, and permits null values and the null key. (The
HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized
and permits nulls.) This class makes no guarantees as to the order of the map; in
particular, it does not guarantee that the order will remain constant over time.
⼏个关键的信息:基于Map接⼝实现、允许null键/值、⾮同步、不保证有序(⽐如插⼊的顺序)、
也不保证序不随时间变化。
在HashMap中有两个很重要的参数,容量(Capacity)和负载因⼦(Load factor)
Initial capacity: The capacity is the number of buckets in the hash table, The initial
capacity is simply the capacity at the time the hash table is created.
Load factor: The load factor is a measure of how full the hash table is allowed to get
before its capacity is automatically increased.
简单的说,Capacity就是bucket的⼤⼩,Load factor就是bucket填满程度的最⼤⽐例。如果对迭
代性能要求很⾼的话不要把capacity设置过⼤,也不要把load factor设置过⼩。当bucket中的
entries的数⽬⼤于capacity*load factor时就需要调整bucket的⼤⼩为当前的2倍。
⼆、构造函数
HashMap底层维护⼀个数组,当新建⼀个HashMap的时候,就会初始化⼀个数组。我们看⼀下
JDK源码中的HashMap构造函数:
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
// Find a power of 2 >= initialCapacity
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
this.loadFactor = loadFactor;
threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
table = new Entry[capacity];
useAltHashing = sun.misc.VM.isBooted() &&
(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
init();
}
可以看到其中⼀⾏为 table = new Entry[capacity]; 。在构造函数中,其创建了⼀个Entry的数
组,其⼤⼩为capacity,那么Entry⼜是什么结构呢?看⼀下源码:
transient Entry<K,V>[] table;
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;
……
}
HashMap中的是通过transient Entry[] table来存储数据,该变量是通过transient进⾏修饰的。
Entry是⼀个static class,其中包含了key和value,也就是键值对,另外还包含了⼀个next的
Entry指针。我们可以总结出:Entry就是数组中的元素,每个Entry其实就是⼀个key-value对,它
持有⼀个指向下⼀个元素的引⽤,这就构成了链表。
大家一起学习丫~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值