1,储存方式
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable{
static final HashMapEntry<?,?>[] EMPTY_TABLE = {};
transient HashMapEntry<K,V>[] table = (HashMapEntry<K,V>[]) EMPTY_TABLE;
transient int size;
int threshold; // 负载因子
final float loadFactor = DEFAULT_LOAD_FACTOR; // 0.75
transient int modCount; // 修改次数
public HashMap(int initialCapacity, float loadFactor) {
...
threshold = initialCapacity;
...
}
static class HashMapEntry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
HashMapEntry<K,V> next;
int hash;
HashMapEntry(int h, K k, V v, HashMapEntry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
}
}
1)内部储存为:HashMapEntry[],而HashMapEntry为明显的链表特征,因此可以记住,HashMap的储存方式为数组+链表
2)HashMap的储存单元:(hash,key,value,bucketIndex)共同存储
2,自增方式
public V put(K key, V value) {
// table为空时,创建table,threshold初始值为4
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
// 获取 key的hash值
int hash = sun.misc.Hashing.singleWordWangJenkinsHash(key);
// 获取 hashcode对Length的求余,Length的值一定是2的次幂
int i = indexFor(hash, table.length);
// 遍历,当key的hashCode和equal相等时,更新数据
for (HashMapEntry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
// 添加数据
modCount++;
addEntry(hash, key, value, i);
return null;
}
private void inflateTable(int toSize) {
// Find a power of 2 >= toSize
int capacity = roundUpToPowerOf2(toSize);
// Android-changed: Replace usage of Math.min() here because this method is
// called from the <clinit> of runtime, at which point the native libraries
// needed by Float.* might not be loaded.
float thresholdFloat = capacity * loadFactor;
if (thresholdFloat > MAXIMUM_CAPACITY + 1) {
thresholdFloat = MAXIMUM_CAPACITY + 1;
}
threshold = (int) thresholdFloat;
table = new HashMapEntry[capacity];
}
/**
* {1 - 1}, {2 - 2}, {3 - 4}, {8 - 8}, {9 - 16}
*
* @param number 输入参数
* @return 返回值,一定为2的次幂
*/
private static int roundUpToPowerOf2(int number) {
// assert number >= 0 : "number must be non-negative";
int rounded = number >= MAXIMUM_CAPACITY
? MAXIMUM_CAPACITY
: (rounded = Integer.highestOneBit(number)) != 0
? (Integer.bitCount(number) > 1) ? rounded << 1 : rounded
: 1;
return rounded;
}
/**
* 等价于 h % length
* @param h hashCode值
* @param length 2的次幂值
* @return 求余值
*/
static int indexFor(int h, int length) {
// assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";
return h & (length - 1);
}
/**
* 添加数据,解决Hash冲突问题
*
* @param hash key的hash值
* @param key key值
* @param value value值
* @param bucketIndex key的hash值,对容量求出的 bucketIndex
*/
void addEntry(int hash, K key, V value, int bucketIndex) {
// 如果达到了最大容量,则扩容;并重新计算bucketIndex和hash值
if ((size >= threshold) && (null != table[bucketIndex])) {
// 扩容
resize(2 * table.length);
hash = (null != key) ? sun.misc.Hashing.singleWordWangJenkinsHash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
/**
* 扩容
* @param newCapacity 新的容量大小,2的次幂值
*/
void resize(int newCapacity) {
HashMapEntry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
HashMapEntry[] newTable = new HashMapEntry[newCapacity];
transfer(newTable);
table = newTable;
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
/**
* Transfers all entries from current table to newTable.
* 注意是所有(包含数组和链表),而不是头部;这是为了解决平摊之后,可能余值不一样的问题
*/
void transfer(HashMapEntry[] newTable) {
int newCapacity = newTable.length;
for (HashMapEntry<K,V> e : table) {
while(null != e) {
HashMapEntry<K,V> next = e.next;
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
/**
* 如果原先数据为空,则增加一条数据
* 如果原先数据不为空,则“旧数据”成为“新数据”的next
* 该处为链表形式
*/
void createEntry(int hash, K key, V value, int bucketIndex) {
HashMapEntry<K,V> e = table[bucketIndex];
table[bucketIndex] = new HashMapEntry<>(hash, key, value, e);
size++;
}
这里的问题很多,面试问的最多的也是这一段,接下来一一回答
1)HashMap的实现原理:利用hashCode值将(hash,key,value, next)共同存储,并采用数组+链表的形势实现。
hash:扩容时,重新计算bucketIndex使用
key、value:储存内容
next:上一次储存的对象
2)如何解决HashCode碰撞:拉链法,即:以数组的节点作为链接点,延伸出一个链表进行存储。
具体储存的方法为:createEntry(...),每次放入都是作为一个链表节点放入
3)大小超过负载因子时:扩容,将原先所有数据从新计算bucketIndex再次安放。
容量:值永远为2的次幂,为了使得(h & length - 1) == h % length公式成立
负载因子:当 存入的值,大于负载因子*容量 时,将会发生 refash(扩容);扩容执行了新建数组,并且将原先数据搬移过去,是很耗费性能的事,而hashCode是散列分布的,在容量快满时,很有可能计算出重复的值。可以分析出,负载因子高时,可能导致链表较长的可能性大,遍历时需要的时间增长;负载因子低时,链表长的可能性很小,但数组会较大;因此0.75为折中方案
4)多线程,同时扩充,发生了条件竞争:可能出现死循环,因此多线程操作,需要线程安全的保障
5)为什么HashMap的容量总是2的次幂:计算BucketIndex时,加快运行是速度。使得(h & length - 1) == h % length成立
3,查询数据
public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}
final Entry<K,V> getEntry(Object key) {
if (size == 0) {
return null;
}
int hash = (key == null) ? 0 : sun.misc.Hashing.singleWordWangJenkinsHash(key);
for (HashMapEntry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
1)先计算出hashCode值,获取到数组中的链表节点;
2)依据链表的头部,依次执行equal方法,找出符合要求的value
4,整体大小
public int size() {
return size;
}
大小,则是通过使用一个全局量记录。需要获取时,获取即可
5,外话
1)和Hashtable对比的影响性能的区别有:(更多的区别,就不列举)
HashMap线程不安全,hashtable线程安全
HashMap容量为2的次幂,hashtable容量不是,因此hashMap在计算bucketIndex时,效率高很多
2)Map的子类中,还有几个有意思的集合,可以去研究
IndentityHashMap:采用数组的偶数储存Key,偶数+1储存Value
TreeMap:采用维持红黑树,从而保障查询数据的效率