final class KeySet extends AbstractSet<K> {
public final int size() { return size; }
public final void clear() { HashMap.this.clear(); }
public final Iterator<K> iterator() { return new KeyIterator(); }
public final boolean contains(Object o) { return containsKey(o); }
public final boolean remove(Object key) {
return removeNode(hash(key), key, null, false, true) != null;
}
public final Spliterator<K> spliterator() {
return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
}
public final void forEach(Consumer<? super K> action) {
Node<K,V>[] tab;
if (action == null)
throw new NullPointerException();
if (size > 0 && (tab = table) != null) {
int mc = modCount;
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next)
action.accept(e.key);
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}
}
HashMap的非静态内部类KeySet
最新推荐文章于 2022-04-25 14:00:33 发布
这个博客详细解析了Java中HashMap的KeySet类的实现细节。KeySet类继承自AbstractSet,实现了size()、clear()、iterator()、contains()、remove()以及spliterator()等方法。它提供了对HashMap键的迭代器,并支持对集合的操作,如遍历、删除等。博客还涵盖了并发修改异常的处理和遍历操作的优化。
摘要由CSDN通过智能技术生成