HashMap的非静态内部类KeySet

这个博客详细解析了Java中HashMap的KeySet类的实现细节。KeySet类继承自AbstractSet,实现了size()、clear()、iterator()、contains()、remove()以及spliterator()等方法。它提供了对HashMap键的迭代器,并支持对集合的操作,如遍历、删除等。博客还涵盖了并发修改异常的处理和遍历操作的优化。
摘要由CSDN通过智能技术生成
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();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值