JDK源码阅读之AbstractSet和AbstractQueue

AbstractSet 提供 Set 接口的骨干实现,从而最大限度地减少了实现此接口所需的工作,此类并没有重写 AbstractCollection 类中的任何实现。 它仅仅添加了 equalshashCode 的实现。

AbstractQueue提供某些 Queue 操作的骨干实现。此类中的实现适用于基本实现 允许包含 null 元素时。

//继承了AbstractCollection接口,实现了Set接口
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {
    protected AbstractSet() {//提供一个默认的构造函数
    }
    //比较是否相等
    public boolean equals(Object o) {
        if (o == this)//比较是否指向同一个值
            return true;

        if (!(o instanceof Set))//类型是否匹配
            return false;
        Collection c = (Collection) o;
        if (c.size() != size())//集合大小是否相等
            return false;
        try {
            return containsAll(c);//通过containsAll方法判断
        } catch (ClassCastException unused)   {
            return false;
        } catch (NullPointerException unused) {
            return false;
        }
    }
    //hashCode方法
    public int hashCode() {
        int h = 0;
        Iterator<E> i = iterator();
        while (i.hasNext()) {
            E obj = i.next();
            if (obj != null)
                h += obj.hashCode();
        }
        return h;
    }
    //删除c里面的所有元素
    public boolean removeAll(Collection<?> c) {
        boolean modified = false;

        if (size() > c.size()) {//比较集合大小是否相等,选择小的执行迭代删除
            for (Iterator<?> i = c.iterator(); i.hasNext(); )//通过迭代器删除
                modified |= remove(i.next());
        } else {
            for (Iterator<?> i = iterator(); i.hasNext(); ) {
                if (c.contains(i.next())) {
                    i.remove();
                    modified = true;
                }
            }
        }
        return modified;
    }
}
//继承了AbstractCollection接口,实现了Queue接口
public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E> {
    protected AbstractQueue() {//实现空的构造函数
    }
    
    public boolean add(E e) {//添加元素,调用offer来实现,调用失败抛出异常
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }

    public E remove() {//删除元素,通过poll方式实现,如果poll返回为空,则抛出异常
        E x = poll();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }

    public E element() {//通过peek来实现,如果peek返回元素为空,则抛出异常
        E x = peek();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }

    public void clear() {//删除所有元素
        while (poll() != null)
            ;
    }
    //批量添加
    public boolean addAll(Collection<? extends E> c) {
        if (c == null)
            throw new NullPointerException();
        if (c == this)
            throw new IllegalArgumentException();
        boolean modified = false;
        for (E e : c)
            if (add(e))
                modified = true;
        return modified;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值