1、romove方法
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?null : e.value; //調用removeNode方法,返回調用方法的值
}
final Node<K,V> removeNode(int hash, Object key, Object value,boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&(p = tab[index = (n - 1) & hash]) != null) { //如果table對應的位置有值
Node<K,V> node = null, e; K k; V v; //node節點就是要替換的節點
if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k)))) //如果所在位置第一個節點的key與刪除的key相等
node = p; //直接替換對應的節點
else if ((e = p.next) != null) { //節點存在下一個節點
if (p instanceof TreeNode) //屬於紅黑樹結構
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
do {
if (e.hash == hash &&((k = e.key) == key ||(key != null && key.equals(k)))) { //key值,hash相等
node = e; //直接替換,退出循環
break;
}
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||(value != null && value.equals(v)))) { //判斷是否找到對應節點
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable); //刪除紅黑樹對應節點
else if (node == p)
tab[index] = node.next; //在節點上值只有一個節點,即第6行的情況
else
p.next = node.next; //直接把刪除節點的前一個節點的next指向刪除節點的nwxt節點,就相當於把刪除節點排除在鏈表之外了
++modCount;
--size;
afterNodeRemoval(node);
return node; //返回node節點
}
}
return null;
}
romoveNode方法是先通過對應的key找到node節點,然後再根據參數value和matchValue的值,這兩個值是在精確刪除key-value鍵值對,如果matchValue是true,並且value不爲空,就會匹配key和value的值,都相等的時纔會刪除對應的節點。
2、replace方法
public V replace(K key, V value) {
Node<K,V> e;
if ((e = getNode(hash(key), key)) != null) { //獲取對應的節點,不爲null就替換value值,返回舊的value值
V oldValue = e.value;
e.value = value;
afterNodeAccess(e);
return oldValue;
}
return null;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&(first = tab[(n - 1) & hash]) != null) { //數組索引位置不爲null
if (first.hash == hash && ((k = first.key) == key || (key != null && key.equals(k))))
return first; //如果第一個節點的key就和對應的key相等,直接返回
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null); //遍歷鏈表或者紅黑樹的節點尋找對應的節點值
}
}
return null;
}
這個方法就是替換對應key的value值,還是一樣,就是先根據key確定在數組中的位置,然後再數組的鏈表或者紅黑樹遍歷查找對應的值,對value進行更改。
3、clear方法
public void clear() {
Node<K,V>[] tab;
modCount++;
if ((tab = table) != null && size > 0) {
size = 0;
for (int i = 0; i < tab.length; ++i) //遍歷數組置爲null
tab[i] = null;
}
}
4、循環迭代
HashMap沒有和ArrayList一樣的iterator()方法,只能通過獲取帶鍵值對的set集合,然後再獲取迭代器。
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es;
return (es = entrySet) == null ? (entrySet = new EntrySet()) : es; //返回一個EntrySet內部類對象
}
final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public final int size() { return size; } //集合的長度
public final void clear() { HashMap.this.clear(); } //清空集合
public final Iterator<Map.Entry<K,V>> iterator() { //返回一個迭代器對象
return new EntryIterator();
}
public final boolean contains(Object o) { //判斷是否包含對應的對象
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Node<K,V> candidate = getNode(hash(key), key);
return candidate != null && candidate.equals(e);
}
public final boolean remove(Object o) { //移除o對象,
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Object value = e.getValue();
return removeNode(hash(key), key, value, true, true) != null;
}
return false;
}
public final Spliterator<Map.Entry<K,V>> spliterator() {
return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0);
}
public final void forEach(Consumer<? super Map.Entry<K,V>> action) { //函數式編程的一個方法
Node<K,V>[] tab;
if (action == null)
throw new NullPointerException();
if (size > 0 && (tab = table) != null) {
int mc = modCount;
for (Node<K, V> e : tab) {
for (; e != null; e = e.next)
action.accept(e);
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}
}
這裏通過entrySet方法返回一個內部類對象,這個entrySet的一個set集合,可以生產一個迭代器對象,如下
HashMap<String,String> map = new HashMap();
Set<Map.Entry<String, String>> entrySet = map.entrySet();
Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
然後就可以循環的遍歷map的鍵值對。也可以只獲取key的集合或者value的集合,也一樣,獲取key的集合使用keySet()方法,返回一個keySet的內部類的對象。獲取values就使用values()方法,返回一個values的內部類對象。如下:
HashMap<String,String> map = new HashMap();
Set<String> keySet = map.keySet();
Iterator<String> keyIterator = keySet.iterator();
Collection<String> values = map.values();
Iterator<String> valueIterator = values.iterator();