HashMap之clear

HashMap之clear


先水一篇
业务逻辑整理

  • 1,当前对象不为空,遍历对象,把每个hash对应下标的对象都设为空
  • 2,modCount是用来计算HashMap的修改次数
/**
 * Removes all of the mappings from this map.
 * The map will be empty after this call returns.
 */
public void clear() {
    Node<K,V>[] tab;
    modCount++;
    if ((tab = table) != null && size > 0) {
        size = 0;
        for (int i = 0; i < tab.length; ++i)
            tab[i] = null;
    }
}