/**
* Offloaded version of put for null keys
*/
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<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;
}
/**
* Adds a new entry with the specified key, value and hash code to
* the specified bucket. It is the responsibility of this
* method to resize the table if appropriate.
*
* Subclass overrides this to alter the behavior of put method.
*/
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
public V get(Object key) {
if (key == null)
return getForNullKey();
int hash = hash(key.hashCode());
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}
/**
* Returns <tt>true</tt> if this map contains a mapping for the
* specified key.
*
* @param key The key whose presence in this map is to be tested
* @return <tt>true</tt> if this map contains a mapping for the specified
* key.
*/
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
/**
* Returns the entry associated with the specified key in the
* HashMap. Returns null if the HashMap contains no mapping
* for the key.
*/
final Entry<K,V> getEntry(Object key) {
int hash = (key == null) ? 0 : hash(key.hashCode());
for (Entry<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;
}
分享到:
相关推荐
HashMap数据结构,HashMap的构造方法,HashMap的put,HashMap的get
看完这篇 HashMap,和面试官扯皮就没问题了 ... - 讲一讲 HashMap put 的全过程 - Hash 函数 - 扩容机制 - 讲一讲 get 方法全过程 - HashMap 的遍历方式 - HashMap 中的移除方法 - 关于 HashMap 的面
背景知识: java1.7 HashMap用的是数组+链表实现的,同时采用的头插入法,存在死循环的问题 java1.8 HashMap用的是数组+链表+... * Implements Map.put and related methods. * * @param key的hash值 * @param key
对HashMap的put方法的源码进行详细解读,分析put方法源码中的内在逻辑关系,欣赏源码独特之美,从中学习更为精致的编程思维
分析HashMap在put数据时是如何找到要存放的位置的,对决定位置的hashCode计算方法进行详细解释,分析为啥要用高低16位异或运算,以及数组长度是如何影响元素存放位置的
HashMap的put逻辑(1.7) .svg
Java数组+链表简单实现HashMap的put和get 数组和链表.pdf
05-HashMap的put操作源码分析(下).mp4
04-HashMap的put操作源码分析(上).mp4
以HashMap的put方法和get方法为出发点,从源码角度,阐述面试的知识点 HashMap中元素被封装成Node对象 static class Node implements Map.Entry { final int hash; final K key; V value; Node next; Node(int ...
下面我们将深入探讨 HashMap 的数据结构、 put 方法的实现细节和 Hash 码的计算过程。 HashMap 的数据结构 HashMap 的数据结构可以分为两部分:数组和链表。数组是 HashMap 的基本结构,链表是数组元素的具体实现...
假设线程A和线程B同时对HashMap进行put操作,并且这时候HashMap的size已经超过了threshold。线程A开始执行resize操作,并将旧的table中的元素重新hash到新的table中。同时,线程B也开始执行resize操作,并将旧的...
3.HashMap的put方法的具体流程? 4.HashMap的扩容操作是怎么实现的? 5.HashMap是怎么解决哈希冲突的? 6.什么是哈希? 7.什么是哈希冲突? 8.HashMap的数据结构? 9.JDK1.8新增红黑树? 10.能否使用任何类作为 Map ...
Java集合专题总结:HashMap和HashTable源码...本文总结了HashMap和HashTable的源码学习和面试总结,涵盖了它们的存储结构、构造方法、get和put方法的源码分析、Hash表的特点和缺点、HashTable和HashMap的区别等内容。
主要hashMap源码,包括put,get等,其中将代码风格修改为更易懂的风格,注释也更简洁明了,可以帮助学习hashMap的同学更简单的学习hashMap
深入理解hashmap、hash算法、理解加载因子、扩容以及get、put方法
当第一次掉用 put() 方法时,数组就会自动的扩容为 16,加载因子为 0.75。 在 jdk1.8 中添加元素时,他会根据添加数据的键与值创建一个 Entry 对象,HashCod() 方法根据 Entry 对象中的键计算出哈希值,再根据哈希...
javaScript模拟的HashMap数据结构,可以方便的put和get。几乎和Java中HashMap类的功能一模一样。非常好用的!
import android.content.ContentProvider; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues;... HashMap localHashMap17 = new HashMap...
HashMap集合非线程安全; HashMap集合继承关系,实现的接口有: public class HashMap extends AbstractMap implements Map, Cloneable, Serializable { } HashMap集合继承了Map集合,实现了Map,Cloneable,...