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

来源:互联网 发布:林珊珊几个淘宝店铺 编辑:程序博客网 时间:2024/05/15 03:36

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)

1 0
原创粉丝点击