java中有hash集合_Java(1.8)集合类中的HashMap
Map接口沒有繼承任何其他接口,它存儲的是Key-Value對,并且Key不能重復。
下面就是Map的所有接口:
Map接口的所有方法
在HashMap 內部每個Key-Value對都用一個Node對象存儲。在Node中保持了key的hash值,Key,Value,和指向下個Node的next變量。
static class Node implements Map.Entry {
final int hash;
final K key;
V value;
Node next;
Node(int hash, K key, V value, Node next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry,?> e = (Map.Entry,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
在HashMap內還有個內部類叫TreeNode,這個其實繼承了Node類。這個類用作紅黑樹的節點。
TreeNode
HashMap用一個數組來保存元素的,每個數組的的數據可以是一個Node組成的鏈表,或者是TreeNode組成的紅黑樹。
transient Node[] table;
在內部還有個重要的變量entrySet, 這個Set對象的用處就是當調用map.keySet()和values()方法時,通過這個entrySet來遍歷所有的元素。
transient Set> entrySet;
看下put(K key, V value)的實現
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
先把key做個Hash計算,如果hashCode 的值小于2的16次方的時候, hash值就是key.hashCode()的值,如果大于,會把高低位進行異或計算。
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
再看方法:putVal,如果第一次添加,就會初始化一個默認16長度的數組(這個邏輯在resize()里面)
添加元素的邏輯:
如果在數組找不到hash對應的元素,就會new一個新的Node并放入table數組相應位置中。
如果數組的里的元素的key 和要插入的key 一直,就把當前value更新
如果查到的元素是個TreeNode類型的,1)如果存在更新value,2)如果不存在,new個新的TreeNode,然后插入的樹中。
如果是個鏈表類型,1)如果存在更新value,2)如果不存在,new個新的Node,然后插入鏈表中。
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node[] tab; Node p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
再看resize()方法,:
首次初始化,數組長度默認是16
如果table已經有元素了,并且當前數組長度的2倍小于最大容量,就把數組擴容到以前的2倍,有了新數組,就把老數據拷貝到新數組。
HashMap里面有個threshold,這個值總比數組的size小,初始化的時候,默認為threshold=16*0.75=12,當HashMap的size大于threshold的時候就要擴容,并非table已經滿了。下次擴容的時候,threshold的大小當前threshold的2倍。
final Node[] resize() {
Node[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node[] newTab = (Node[])new Node[newCap];
table = newTab;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
((TreeNode)e).split(this, newTab, j, oldCap);
else { // preserve order
Node loHead = null, loTail = null;
Node hiHead = null, hiTail = null;
Node next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java中有hash集合_Java(1.8)集合类中的HashMap的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java zmq订阅_从ZMQ PUB套
- 下一篇: java中flush 函数,Java D