Java1.8源码集合类学习UML图——Collection接口&AbstractCollection抽象类

Collection接口&AbstractCollection类

Collection&AbstractCollection
可以明显地看到Collection接口继承自Iterable接口,而AbstractCollection抽象类又是Collection的子类,那么AbstractCollection中的方法实现就可以使用迭代器和“for-each”语法。

Collection接口声明的方法

该接口中声明了集合常用的add(),remove(),clear(),size()等方法外,有几个重要的方法在这也给出了声明。

boolean equals(Object o)  int hashcode();

equals()是判断两个对象是否相等,而这个相等的概念是由hash值决定的,也就是说c1.equals(c2)等价于c1.hashcode()==c2.hashcode()

Object[] toArray()  T[] toArray(T[] a);

这两个方法是数组与集合类的桥梁,特别是转到数组时,大小越界问题。

AbstractCollection中部分方法的具体实现

boolean contains(Object o)

 /**
     * {@inheritDoc}
     *
     * <p>这个实现遍历集合中的所有元素,检查是否等特定的元素o。
     * @throws ClassCastException   {@inheritDoc}
     * @throws NullPointerException {@inheritDoc}
     */
    public boolean contains(Object o) {
        Iterator<E> it = iterator();
        if (o==null) {
            while (it.hasNext())
                if (it.next()==null)
                    return true;
        } else {
            while (it.hasNext())
                if (o.equals(it.next()))
                    return true;
        }
        return false;
    }

Object[] toArray()

/**<p>这个方法等价于:
     *
     *  <pre> {@code
     * List<E> list = new ArrayList<E>(size());
     * for (E e : this)
     *     list.add(e);
     * return list.toArray();
     * }</pre>
     */
     public Object[] toArray() {
        // Estimate size of array; be prepared to see more or fewer elements
        Object[] r = new Object[size()];//试着调用size()方法,这个是可选操作
        Iterator<E> it = iterator();
        for (int i = 0; i < r.length; i++) {
            if (! it.hasNext()) // fewer elements than expected
                return Arrays.copyOf(r, i);
            r[i] = it.next();
        }
        return it.hasNext() ? finishToArray(r, it) : r;
    }

boolean remove(Object o)

/**
     * {@inheritDoc}
     *
     * <p>s使用迭代器方法遍历整个集合,找到特定的元素,删除。
     * <p>Note that this implementation throws an
     * <tt>UnsupportedOperationException</tt> if the iterator returned by this
     * collection's iterator method does not implement the <tt>remove</tt>
     * method and this collection contains the specified object.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws ClassCastException            {@inheritDoc}
     * @throws NullPointerException          {@inheritDoc}
     */
    public boolean remove(Object o) {
        Iterator<E> it = iterator();
        if (o==null) {
            while (it.hasNext()) {
                if (it.next()==null) {
                    it.remove();
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (o.equals(it.next())) {
                    it.remove();
                    return true;
                }
            }
        }
        return false;
    }

还有一些批量操作

判断一个集合是否在内boolean containsAll(Collection<?> c)
添加一个集合boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}

移除一个集合boolean removeAll(Collection<?> c)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值